Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: best way to conditionally insert a row
Mark Harrison 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
Another option might be to use the MERGE statement. Normally, this is used to INSERT values on some conditions and UPDATE on others. But you omit the WHEN MATCHED UPDATE clause and just do the INSERT.
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9016.htm#i2081218
This option is only available in Oracle 9i and higher.
HTH,
Brian
-- =================================================================== Brian Peasland dba_at_nospam.peasland.net http://www.peasland.net Remove the "nospam." from the email address to email me. "I can give it to you cheap, quick, and good. Now pick two out of the three" - Unknown -- Posted via a free Usenet account from http://www.teranews.comReceived on Thu Aug 09 2007 - 08:04:56 CDT
![]() |
![]() |