Solved

MSSQL TSQL - Insert where not exists

Posted on 2010-11-26
4
1,291 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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now