[GENERAL] Re: Dynamic use of RAISE with USING to generate and catchnon-hardcoded custom exceptions

Поиск
Список
Период
Сортировка
От mike davis
Тема [GENERAL] Re: Dynamic use of RAISE with USING to generate and catchnon-hardcoded custom exceptions
Дата
Msg-id HE1P190MB0395EB5B479AA5451A01C00B81670@HE1P190MB0395.EURP190.PROD.OUTLOOK.COM
обсуждение исходный текст
Ответ на Re: [GENERAL] Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general

>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



В списке pgsql-general по дате отправления:

Предыдущее
От: Vladimir Mihailenco
Дата:
Сообщение: [GENERAL] shared_buffers smaller than max_wal_size
Следующее
От: Imre Samu
Дата:
Сообщение: Re: [GENERAL] Performance appending to an array column