Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
user786
Starting Member
1 Post |
Posted - 2011-11-08 : 13:43:42
|
| How can i write SQL INSERT IntO Statement that will add in the database only new values in the database, and skip the ones already in the database?Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Just_Jeff
Starting Member
7 Posts |
Posted - 2011-11-09 : 19:41:50
|
| Are you talking about only inserting one row at a time... like for data entry screens?If so then you could check for matching records using the Exists() function.Here is my example (assume that you have put the value to be checked in a variable, in this case @NewName):declare @NewName varchar(50)Set @NewName = 'Amazon'if not exists(Select * from tblCompany where CompanyName = @NewName) insert into tblCompany (CompanyName) values (@NewName)If you are looking for a multi-record solution then you could use a select statement in as the criteria for a Contains function like this:Insert into tblTest1 (Thing1)select Thing2from tblTest2where Thing2 not in (select Thing1 from tblTest1)Hope that helps!-Jeff |
 |
|
|
|
|
|