Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: best way to conditionally insert a row
On 9 août, 02:05, Mark Harrison <m..._at_pixar.com> wrote:
> I would like to insert a row into a table, but only
> if the primary key does not exist.
>
> For example, if I have a table "keywords" with
> columns "name" and "val", I would like
>
> insert into keywords (name, val) values ('color', 'blue');
>
> to happen only if there is not already a row where
> name='color'.
>
> What is the best way to do this?
>
> Many TIA!
> Mark
What about the lazy way, by handling ORA-00001? PL/SQL exception DUP_VAL_ON_INDEX if you use PL/SQL here.
That's not really nice, but your code will be simpler in the end. Received on Thu Aug 09 2007 - 10:44:26 CDT