Обсуждение: Get id of a tuple using exception

Поиск
Список
Период
Сортировка

Get id of a tuple using exception

От
f vf
Дата:
Hello,<br />i'm using a pl/sql procedure and I prevent inserting duplicate tuples using an exception for example:<br
/><br/>  BEGIN<br />       INSERT INTO "Triples"(id, subject, predicate, "object") <br />                    VALUES
(id,sub_i, pred_i, obj_i);<br /><b> EXCEPTION WHEN unique_violation THEN<br />                --do something.<br /><br
/></b>Insome 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
triplealready existing in the table?<br /><br />Thanks, <br />Filipe<br /> 

Re: Get id of a tuple using exception

От
Adrian Klaver
Дата:
On Thursday, April 14, 2011 3:56:51 am f vf wrote:
> 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 id is the PRIMARY KEY then it would be the same as the id you tried to 
INSERT correct?

> 
> Thanks,
> Filipe

-- 
Adrian Klaver
adrian.klaver@gmail.com


Re: Get id of a tuple using exception

От
Adrian Klaver
Дата:
On Thursday, April 14, 2011 6:50:57 am f vf wrote:
> No, the id is nextval( 'triples_seq'), if I do nothing, so its something
> like:
>   BEGIN
>         INSERT INTO "Triples"(id, subject, predicate, "object")
>                      VALUES (nextval( 'triples_seq'), sub_i, pred_i,
> obj_i); EXCEPTION WHEN unique_violation THEN
>               --do something.
> 
>  The unique constraint is applyied to the subject, predicate and "object"
> fields. So, if I try to insert anything that has these 3 fields equal to
> any tuple that already exists in the table I want to get the id of the
> original tuple.
> 

To your original question I am not aware of a way of returning the id of the 
offending tuple,  other than through a SELECT.
-- 
Adrian Klaver
adrian.klaver@gmail.com


Re: Get id of a tuple using exception

От
Jasen Betts
Дата:
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