PostgreSQL/Postgre SQL/RAISE EXCEPTION

Материал из SQL эксперт
Перейти к: навигация, поиск

Using RAISE EXCEPTION

postgres=#
postgres=# -- Name: "raise_test" () Type: FUNCTION Owner: postgres
postgres=# CREATE FUNCTION "raise_test" () RETURNS integer AS "
postgres"#   DECLARE
postgres"#
postgres"#      -- Declare an integer variable for testing.
postgres"#
postgres"#     an_integer INTEGER = 1;
postgres"#
postgres"#   BEGIN
postgres"#
postgres"#      -- Raise a debug level message.
postgres"#
postgres"#     RAISE DEBUG ""The raise_test() function began."";
postgres"#
postgres"#     an_integer = an_integer + 1;
postgres"#
postgres"#      -- Raise a notice stating that the an_integer
postgres"#      -- variable was changed, then raise another notice
postgres"#      -- stating its new value.
postgres"#
postgres"#     RAISE NOTICE ""Variable an_integer was changed."";
postgres"#     RAISE NOTICE ""Variable an_integer value is now %."",an_integer;
postgres"#
postgres"#      -- Raise an exception.
postgres"#
postgres"#     RAISE EXCEPTION ""Variable % changed.  Aborting transaction."",an_integer;
postgres"#
postgres"#   END;
postgres"# " LANGUAGE "plpgsql";
CREATE FUNCTION
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=#
postgres=#