After reading the documentation for ASSERT, I am still confused how to use it, and can't find any examples online of how I would do something simple using ASSERT
in a .sql script.
For example, say I want to ASSERT
that the number of rows returned from SELECT * FROM my_table WHERE my_col = 3
is equal to 10.
Can someone provide a working example of that?
The ASSERT statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions. ASSERT condition [ , message ]; The condition is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT statement does nothing further.
Users can control where these error messages will be reported (i.e., on client screen, server logs or on both) by setting the postgresql. conf parameters “log_min_messages” and “client_min_messages.” Format specifies the error message that the user wants to display.
The code that follows serves to illustrate this point. RAISE INFO 'Hello World !'; RAISE NOTICE '%', variable_name; RAISE NOTICE 'Current value of parameter (%)', my_var; RAISE EXCEPTION '% cannot have null salary', EmpName; Raise Notice In Postgresql Function.
if it is an user defined exception, delete the corresponding declaration and specify unique error code via ERRCODE in a USING clause. in catch-block replace SQLCODE by SQLSTATE.
I would assume you try todo smth similar?
so=# select count(*) from pg_database;
count
-------
21
(1 row)
so=# do $$ begin assert (select count(*) from pg_database) = 21, 'not 21!';end;$$;
DO
so=# do $$ begin assert (select count(*) from pg_database) = 22, 'not 22!';end;$$;
ERROR: not 22!
CONTEXT: PL/pgSQL function inline_code_block line 1 at ASSERT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With