[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

conditional insert using exists

Posted on 2004-08-24
5
Medium Priority
?
610 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

656 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