- pl/pgSQL - How can I intercept an eventual exception and put it in a text ?

Поиск
Список
Период
Сортировка
От Bruno Baguette
Тема - pl/pgSQL - How can I intercept an eventual exception and put it in a text ?
Дата
Msg-id !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAA9NF9Wvbgz0m6V6WcrOnuoAEAAAAA@netcourrier.com
обсуждение исходный текст
Список pgsql-general
Hello,

Here's a pl/pgSQL function that simply add a two values inside a table.

CREATE function tuple_add(VARCHAR(200)) RETURNS text AS
'
DECLARE
Response text;
PeopleName ALIAS FOR $1;
BEGIN
INSERT INTO PEOPLES VALUES (nextval(''sequence_id''),PeopleName);
return Response;
END;
'   LANGUAGE 'plpgsql';

If the user try to add the same value (for the PeopleName field),
PostGreSQL will throw an exception and print "ERROR: Cannot insert a
duplicate key into unique index biblio_maisonsediti_libelle_key ".

How can I intercept ANY exception inside the function, and to put in the
"Response" variable the text of the EXCEPTION ?

In other words, I want to avoid to have my function to throw any
exception, so I want to intercept an eventual exception and to take the
text ("ERROR: Cannot insert a duplicate key into unique index
biblio_maisonsediti_libelle_key" for example) and to put it in the
"Response" variable so the program that call my function will receive
the error message.

Thanks in advance for your help !  :-)

-------------------------------------------------
Bruno BAGUETTE - bruno.baguette@netcourrier.com


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

Предыдущее
От: Don Isgitt
Дата:
Сообщение: db design help needed
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Optimizing suggestions