Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: newbie -- best way to Insert where not exists

Re: newbie -- best way to Insert where not exists

From: Spencer <spencerp_at_swbell.net>
Date: Wed, 20 Dec 2000 21:06:10 -0600
Message-ID: <ase06.419$Am1.91113@nnrp3.sbc.net>

create a unique index on the table (an enabled primary key constraint, for example), then use a pl/sql procedure to catch the exception:

begin
  insert into tabx (col1) values 'myValue'; exception
  when dup_val_on_index then
    null;
end;

<jimbo1155_at_my-deja.com> wrote in message news:91r601$lea$1_at_nnrp1.deja.com...
> I'm inserting records in a loop, but it's possible that
> some keys exist already, so I want to code one statement that
> will insert the record only if it doesn't exist.
>
> So I came up with:
>
> insert into myTable select 'myValue' from dual
> where not exists (select * from myTable where mykey = 'myValue')
>
> Does this look like the right way to do this?
>
>
> Sent via Deja.com
> http://www.deja.com/
>
Received on Wed Dec 20 2000 - 21:06:10 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US