?
Solved

conditional insert using exists

Posted on 2004-08-24
5
Medium Priority
?
606 Views
Last Modified: 2008-02-01
From a VB front end, I am trying to do a contional insert on a table as follows (this is a one time update, so performance is not an issue)

insert into table_1 values ('gary','gary','gary') if
      not exists (select * from table_1 where col1 = 'gary' and
            col2 = 'gary' and col3 = 'gary')

it keeps adding rows even if the record already exists

select * from table_1 returns 6 records including one that looks like   gary,gary,gary

if exists (select * from table_1 where col1 = 'gary' and
            col2 = 'gary' andcol3 = 'gary') print 'yes'     returns     yes

thank you!
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
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
billy21 earned 2000 total points
ID: 11883232
try something like this...
insert into table_1
Select 'gary','gary','gary'
Where not exists (select * from table_1 where col1 = 'gary' and
          col2 = 'gary' and col3 = 'gary')
0
 
LVL 5

Author Comment

by:gary_j
ID: 11883391
it worked, thank you very much!

why didn't mine work, though?  -- just curious
0
 
LVL 6

Expert Comment

by:billy21
ID: 11883445
Yours saw this line

if not exists (select * from table_1 where col1 = 'gary' and
          col2 = 'gary' and col3 = 'gary')


As a separate tsql statement.  You can't use 'if' in a query. ;-)
0
 
LVL 6

Expert Comment

by:billy21
ID: 11883508
That is sql server thought you were doing this...

insert into table_1
values ('gary','gary','gary')


if not exists (select * from table_1 where col1 = 'gary' and col2 = 'gary' and col3 = 'gary')
--With nothing following the condition which is perfectly legal
0
 
LVL 5

Author Comment

by:gary_j
ID: 11883677
okay -- thanks

if you have a minute, then, why did this (which was my original attempt) fail?  It returns "incorrect syntax near the keyword 'where'"

insert into table_1 values ('gary','gary','gary')
      where not exists (select * from table_1 where col1 = 'gary' and
            col2 = 'gary' and col3 = 'gary')
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.
Suggested Courses

800 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