Solved

Insert into MSSQL database if teh record does not already exist

Posted on 2014-04-10
4
444 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
[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
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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 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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
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: …

724 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