Solved

conditional insert using exists

Posted on 2004-08-24
5
596 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 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