Search Postgresql Archives

Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>This works for me:
>
>DO $$
>DECLARE
 > v_msg TEXT := 'SOMETHING IS WRONG';
>  v_sqlstate TEXT := 'E0001';
>BEGIN
>  RAISE EXCEPTION USING message = v_msg, errcode = v_sqlstate;
>EXCEPTION
>  WHEN SQLSTATE 'E0001' THEN
>     RAISE NOTICE '%','Error E0001 raised - going to do something about it';
>  WHEN OTHERS THEN
>     RAISE NOTICE 'OTHER ERRORS: %,%', sqlstate,sqlerrm;
>END$$;

>NOTICE:  Error E0001 raised - going to do something about it
>
>Or you could do
>  RAISE EXCEPTION SQLSTATE v_sqlstate USING message = v_msg;

That does indeed work !

The second possible way of :
RAISE EXCEPTION SQLSTATE v_sqlstate USING message = v_msg;

however doesn't ! I think that was the format i had also tried and why i went down the dymanic route.

So it seems variables can be used in the USING subclause but not outside it. The manual does seem to hint at this as
"after level if any, you can write a format (which must be a simple string literal, not an _expression_)"

Anyway,  RAISE EXCEPTION USING message = v_msg, errcode = v_sqlstate; works a treat!

Many thanks Tom & Pavel.

Mike




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux