Solved

conditional insert using exists

Posted on 2004-08-24
5
592 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
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
billy21 earned 500 total points
Comment Utility
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
Comment Utility
it worked, thank you very much!

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

Expert Comment

by:billy21
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

762 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

10 Experts available now in Live!

Get 1:1 Help Now