Using the RAISE statement
postgres=#
postgres=#
postgres=# -- Using the RAISE statement
postgres=#
postgres=# CREATE FUNCTION raise_test () RETURNS integer AS '
postgres'# DECLARE
postgres'# -- Declare an integer variable for testing.
postgres'# an_integer INTEGER = 1;
postgres'# BEGIN
postgres'# -- Raise a debug level message.
postgres'# RAISE DEBUG ''The raise_test() function began.'';
postgres'# an_integer = an_integer + 1;
postgres'#
postgres'# -- Raise a notice stating that the an_integer variable was changed,
postgres'# -- then raise another notice stating its new value.
postgres'# RAISE NOTICE ''Variable an_integer was changed.'';
postgres'# RAISE NOTICE ''Variable an_integer's value is now %.'',an_integer;
ERROR: syntax error at or near "s" at character 501
LINE 13: RAISE NOTICE ''Variable an_integer's value is now %.'',a...
^
postgres=#
postgres=# -- Raise an exception.
postgres=# RAISE EXCEPTION ''Variable % changed. Transaction aborted.'',an_integer;
ERROR: syntax error at or near "RAISE" at character 1
LINE 1: RAISE EXCEPTION ''Variable % changed. Transaction aborted.'...
^
postgres=#
postgres=# RETURN 1;
ERROR: syntax error at or near "RETURN" at character 1
LINE 1: RETURN 1;
^
postgres=# END;
WARNING: there is no transaction in progress
COMMIT
postgres=#
postgres=#
postgres=# SELECT raise_test();
NOTICE: Variable an_integer was changed.
NOTICE: Variable an_integer value is now 2.
ERROR: Variable 2 changed. Aborting transaction.
postgres=#
Related examples in the same category