Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MSSQL TSQL - Insert where not exists

Posted on 2010-11-26
4
Medium Priority
?
1,316 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
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

927 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