• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1345
  • Last Modified:

conditional insert using exists (AGAIN!)

This statement returns an "Incorrect syntax near the keyword WHERE" error, and I don't understand why ...

INSERT INTO atable VALUES (list of column values) WHERE NOT EXISTS
      (SELECT * FROM thesametable WHERE col_1 = 'something' And col_2 = 'somethingelse')

I know it has something to do with EXISTS; I confirmed that by removing the WHERE clause from the SELECT statement.

0
gary_j
Asked:
gary_j
  • 2
1 Solution
 
Scott PletcherSenior DBACommented:

IF NOT EXISTS(SELECT * FROM thesametable WHERE col_1 = 'something' And col_2 = 'somethingelse')
    INSERT INTO atable VALUES (list of column values)
0
 
Scott PletcherSenior DBACommented:
Or you can do this:

INSERT INTO atable
SELECT (list of column values)
WHERE NOT EXISTS(SELECT * FROM thesametable WHERE col_1 = 'something' And col_2 = 'somethingelse')
0
 
gary_jAuthor Commented:
thank you very much!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now