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


Insert into MSSQL database if teh record does not already exist

Posted on 2014-04-10
Medium Priority
Last Modified: 2014-04-10

i have a .net application and i am trying to insert a value only if it does not already exist in teh table but every timei run it get get duplicated data

        sql.CommandText = "  insert into CC_eCollegeTermIDList   (TermCode,TermPart,eCollegeTermIDWebBased,eCollegeTermIDWebEnhanced,UpdatedUser,DateUpdated) Values " & " (@Term, @TermPart,'','','AutomatedSystem',@DateUpdate)" & _
 " Select @Term,@TermPart Where not exists(select * from CC_eCollegeTermIDList where TermPart = @TermPart and TermCode = @Term)"
Question by:Chris Jones
  • 2
  • 2
LVL 41

Accepted Solution

Sharath earned 2000 total points
ID: 39992737
check this.
        sql.CommandText = "  insert into CC_eCollegeTermIDList (TermCode,TermPart,eCollegeTermIDWebBased,eCollegeTermIDWebEnhanced,UpdatedUser,DateUpdated) " & _
 " Select @Term,@TermPart,'','','AutomatedSystem',@DateUpdate Where not exists(select * from CC_eCollegeTermIDList where TermPart = @TermPart and TermCode = @Term)"

Open in new window


Author Comment

by:Chris Jones
ID: 39992759
oh wow thanks a million what was i doing wrong it almost looks the same ?
LVL 41

Assisted Solution

Sharath earned 2000 total points
ID: 39992859
You have two SQL statements in your code. One INSERT with VALUES clause and another SELECT statement. So its just inserting the record and then running the SELECT statement.
What you need is one INSERT statement which checks if the values exist or not. I just removed the VALUES clause.

Author Closing Comment

by:Chris Jones
ID: 39993308
thanks one little thing can throw everything off

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

885 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