Solved

conditional insert using exists (AGAIN!)

Posted on 2004-08-30
3
1,334 Views
Last Modified: 2008-01-09
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
Comment
Question by:gary_j
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 11932480

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

Expert Comment

by:ScottPletcher
ID: 11932496
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
 
LVL 5

Author Comment

by:gary_j
ID: 11932518
thank you very much!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now