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

From: "Bruno Baguette" <bruno(dot)baguette(at)netcourrier(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: - pl/pgSQL - How can I intercept an eventual exception and put it in a text ?
Date: 2002-06-22 20:12:59
Message-ID: !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAA9NF9Wvbgz0m6V6WcrOnuoAEAAAAA@netcourrier.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)baguette(at)netcourrier(dot)com

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-06-22 21:48:08 Re: Optimizing suggestions
Previous Message Don Isgitt 2002-06-22 20:01:06 db design help needed