conditional insert using exists (AGAIN!)

Posted on 2004-08-30
Medium Priority
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.

Question by:gary_j
  • 2
LVL 70

Accepted Solution

Scott Pletcher earned 2000 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)
LVL 70

Expert Comment

by:Scott Pletcher
ID: 11932496
Or you can do this:

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

Author Comment

ID: 11932518
thank you very much!

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

627 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