Solved

MSSQL TSQL - Insert where not exists

Posted on 2010-11-26
4
1,303 Views
Last Modified: 2012-05-10
Hi,

I've trint to write some SQL to insert into a MSSQL database via asp - I trying to prevent duplicate rows being inserted into the database -  but the following example keeps failing for some reason?

Insert into ProdFeatures (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14) Values ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14') Where Not Exists (select * from ProdFeatures where f1 = '1' and f12 = '12')

Microsoft OLE DB Provider for SQL Server error '80040e14'

Incorrect syntax near the keyword 'Where'.

As always, any help is greatly appreciated.

Many thanks

JamWales
0
Comment
Question by:Jamie
[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
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 34219569
Change to select form, not values

Insert into ProdFeatures (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14)
select '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'
Where Not Exists
(select * from ProdFeatures where f1 = '1' and f12 = '12')
0
 
LVL 9

Expert Comment

by:TonyReba
ID: 34219647
I suggest you to troubleshoot your select statement and see if that returns the data you need..
sql = select * from ProdFeatures where f1 = '1' and f12 = '12'

0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 34219664
Try this one.......

If not exists (select * from ProdFeatures where f1 = '1' and f12 = '12')
begin
Insert into ProdFeatures (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14) Values ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14')
end
0
 

Author Closing Comment

by:Jamie
ID: 34221004
Hi cyberkiwi:

Many thanks for your solution - works perfectly - I did try using select from sys.dual but this didn't work as it was an oracle command!  

Thanks to everyone else for your suggestions, but this was the best solution for my particular case.

Regards

Jamie
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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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