Improve company productivity with a Business Account.Sign Up

x
?
Solved

Insert into MSSQL database if teh record does not already exist

Posted on 2014-04-10
4
Medium Priority
?
471 Views
Last Modified: 2014-04-10
Hello,  

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)"
0
Comment
Question by:Chris Jones
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
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

0
 
LVL 1

Author Comment

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

Assisted Solution

by:Sharath
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.
0
 
LVL 1

Author Closing Comment

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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

588 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