Solved

conditional insert using exists

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 32
Query Help - MSSQL - Averages 5 27
Unable to Uninstall Visual Studio 2015 7 26
T-SQL:  Collapsing 9 25
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

773 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