Re: Get id of a tuple using exception - Mailing list pgsql-sql

From Jasen Betts
Subject Re: Get id of a tuple using exception
Date
Msg-id ioc14d$53p$2@reversiblemaps.ath.cx
Whole thread Raw
In response to Get id of a tuple using exception  (f vf <fvalentef@gmail.com>)
List pgsql-sql
On 2011-04-14, f vf <fvalentef@gmail.com> wrote:
> --000e0cd2bf6a60c30804a0dec84b
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hello,
> i'm using a pl/sql procedure and I prevent inserting duplicate tuples using
> an exception for example:
>
>   BEGIN
>        INSERT INTO "Triples"(id, subject, predicate, "object")
>                     VALUES (id, sub_i, pred_i, obj_i);
> * EXCEPTION WHEN unique_violation THEN
>                 --do something.
>
> *In some cases I have interest in getting the id of the tuple that was
> already in the table when the exception is triggered. Is there a way for the
> EXCEPTION to return that id instead of using a select to know wich was the
> id of the triple already existing in the table?

if the unique violation is on the ID column that's easy, if it's on
some other constraint then no there's no way to get the id.

do a select first looking for the colliding row

then fall back to an insert.

there may be weaknesses with this, it depends on why you need the Id.


-- 
⚂⚃ 100% natural



pgsql-sql by date:

Previous
From: Jasen Betts
Date:
Subject: Re: convert in GMT time zone without summer time
Next
From: LaraK
Date:
Subject: Re: convert in GMT time zone without summer time