Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.tools -> Re: Newbie want to insert a row ...
depends on how many errors you expect... It could be more efficient to try and insert because not mary duplicates are ever attempted, or -when many duplicates are offered- to check for existance first.
3:
a) define cursor (similar to your case 2) b) open it c) if %found exit, else insert.
-- Frank Newbie <midifree_at_free.fr> schreef in berichtnieuws J1jy5.1469$kR.4895881_at_nnrp2.proxad.net...Received on Thu Sep 21 2000 - 00:00:00 CDT
> Hi,
> I work on PL/SQL and i want to insert a row if it's not yet inserted :
>
> MyTable(PrimaryKey, Field)
>
> What's the best solution to insert a row if it's not yet inserted in this
> table and is there better solution ?
> 1:
> BEGIN
> INSERT INTO MyTable(PrimaryKey, Field) Values ('KeyValue',
> 'FieldValue');
> EXCEPTION
> WHEN DUP_VAL_ON_INDEX THEN
> NULL;
> END;
>
> 2 : -- I think that this solution can don't work properly sometime :
> DECLARE
> Cpt NUMBER;
> BEGIN
> SELECT COUNT(0) INTO Cpt FROM MyTable WHERE PrimaryKey = 'KeyValue';
> IF Cpt = 0 THEN
> INSERT INTO MyTable(PrimaryKey, Field) Values ('KeyValue',
> 'FieldValue');
> END IF;
> END;
>
> 3 : ????
>
> Thanks a lot
>
>