Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: newbie -- best way to Insert where not exists
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