0

I have the following query to insert a record if it does not already exists:

INSERT INTO "BMAN_TP1"."CELLS_TEXT" ("SET_ID", "CELL_ID")
VALUES (291565, 4256)

WHERE NOT EXISTS ( 
    SELECT "SET_ID", "CELL_ID"
    FROM "BMAN_TP1"."CELLS_TEXT"
    WHERE ("SET_ID"=291565)
    AND ("CELL_ID"=4256)
)

I know it can't work, because you can't use WHERE NOT EXISTS clause with INSERT INTO VALUES, but only with INSERT INTO SELECT (...).

Is there any other way to achieve this?

PLEASE NOTE:
I can't use Oracle's SELECT 291565, 4256 FROM DUAL because it should also work with SQL Server.

I can't use MERGE for the same reason.

Teejay
  • 7,210
  • 10
  • 45
  • 76
  • possible duplicate of [oracle insert if row not exists](http://stackoverflow.com/questions/3147874/oracle-insert-if-row-not-exists) – Ben Nov 06 '13 at 13:42
  • As I said, Oracle custom statements cannot be a solution for me. – Teejay Nov 06 '13 at 13:45
  • 1
    `MERGE` also exists on MS SQL Server since version 2008, see [this MSDN entry](http://msdn.microsoft.com/en-us/library/bb510625.aspx) – Marco Baldelli Nov 06 '13 at 13:57
  • Ok, didn't know about that. Let's say i do not want to use `MERGE`, still looking for something more general. – Teejay Nov 06 '13 at 14:24
  • This StackOverflow thread has the answer - use the MERGE function [Oracle insert if not exists statement](http://stackoverflow.com/questions/10824764/oracle-insert-if-not-exists-statement) – Mike Nov 06 '13 at 13:39

0 Answers0