Re: parsing SQLERRM ?

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: <david(dot)sahagian(at)emc(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: parsing SQLERRM ?
Date: 2012-06-15 08:31:11
Message-ID: D960CB61B694CF459DCFB4B0128514C20800BCFA@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

david(dot)sahagian(at)emc(dot)com wrote:
> (version == 9.1)
>
> In my PL/pgSQL stored functions,
> I want to be able to distinguish which FK-constraint caused the
[foreign_key_violation] exception.
> . . .
> BEGIN
> delete from MY_COOL_TABLE where id = 123 ;
> EXCEPTION
> WHEN foreign_key_violation THEN
> CASE
> WHEN (SQLERRM tells me it blew up because of FK X) THEN . . .
;
> WHEN (SQLERRM tells me it blew up because of FK Y) THEN . . .
;
> WHEN (SQLERRM tells me it blew up because of FK Z) THEN . . .
;
> END;
> WHEN others THEN
> raise;
> END;
> . . .
>
> Is a "robust enough" parsing of SQLERRM actually the best/only way to
determine this ?

I think so.

Not that it is particularly nice, though. It should be fairly robust to
search for the name of the constraint in the error message.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vishwas Dwivedi 2012-06-15 13:16:31 Issue in save and retreive file in postgres
Previous Message Jeff Davis 2012-06-15 07:02:45 Re: Reference with inheritance propagate data