Solved

conditional insert using exists (AGAIN!)

Posted on 2004-08-30
3
1,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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:Scott Pletcher
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

729 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