Solved

t-sql insert into table if not exist

Posted on 2013-05-10
6
1,006 Views
Last Modified: 2013-05-11
I've got 1 table

[Src Table]
  [ID No]
  [Date]
  [Selected]

[Dest Table]
  [ID No]
  [Date]
  [Value]
  [Selected]


I want to insert all the data from the Src Table to the dest table if the [ID No] and [Date] combination does not already exists in Dest Table.
0
Comment
Question by:koossa
[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
6 Comments
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39155255
insert into [dest table] (id no, etc..) select id no, etc.. from [src table] where [src table].[id no] NOT in (select [ID No] from [Dest Table]

The code isn't usable so you can't copy and paste but i hope you get the idea, let me know if you need fully working code.
0
 
LVL 12

Accepted Solution

by:
Jared_S earned 250 total points
ID: 39155261
There are a few ways to do it, but using a Not Exists clause like this will perform better than the others...

INSERT INTO [Dest Table] ([ID No], [Date], [Selected])
SELECT [ID No], [Date], [Selected]
FROM [Src Table] s
WHERE NOT EXISTS (SELECT 1 FROM [Dest Table] t
WHERE t. [ID No] = s.[ID No] and t.[Date]=s.[Date])
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 250 total points
ID: 39155271
INSERT INTO [Dest Table] d
([ID No], [Date],[Selected])
SELECT  s.[ID No], s.[Date], s.[Selected]
FROM [Src Table] s WHERE
 not exists (SELECT ID FROM [Src Table] s2 WHERE s2.ID=s.ID AND s2.Date=s.Date)

Try something like the above

You may have to put a d. in front of
([ID No], [Date],[Selected]) like this
(d.[ID No], d.[Date],d.[Selected])
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 25

Expert Comment

by:SStory
ID: 39155272
Guess we posted at the same time.
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39155286
I created and example script as sqlfiddle so you can have a play

http://sqlfiddle.com/#!3/261be/6


Create Table SRCTABLE
(IDNo int,
 Date varchar(30),
 Selected varchar(30))

INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('1','16/12/2012 00:00:00','Test1');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('2','15/12/2012 00:00:00','Test2');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('3','31/05/2012 00:00:00','Test3');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('4','31/05/2012 00:00:00','Test4');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('5','23/08/2012 00:00:00','Test5');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('6','21/04/2012 00:00:00','Test6');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('7','27/02/2012 00:00:00','Test7');
INSERT INTO SRCTABLE(IDNo,Date,Selected) VALUES('8','21/09/2012 00:00:00','Test8');

Create Table DESTTABLE
(IDNo int,
 Date varchar(30),
 value int,
 Selected varchar(30))
  
  

INSERT INTO DESTTABLE(IDNo,Date,Value,Selected) VALUES('1','16/12/2012 00:00:00','456','Test1');
INSERT INTO DESTTABLE(IDNo,Date,Value,Selected) VALUES('2','15/12/2012 00:00:00','456','Test2');
INSERT INTO DESTTABLE(IDNo,Date,Value,Selected) VALUES('3','31/05/2012 00:00:00','456','Test3');
INSERT INTO DESTTABLE(IDNo,Date,Value,Selected) VALUES('4','31/05/2012 00:00:00','456','Test4');
INSERT INTO DESTTABLE(IDNo,Date,Value,Selected) VALUES('5','23/08/2012 00:00:00','456','Test5');

Open in new window



INSERT INTO DESTTABLE ([IDNo], [Date], [Selected])
SELECT [IDNo], [Date], [Selected]
FROM [SRCTABLE] st 

WHERE NOT EXISTS 

(SELECT * FROM [DESTTABLE] DT
WHERE DT. [IDNo] = st.[IDNo] and DT.[Date]=st.[Date]);

Select * from DESTTABLE

Open in new window

0
 

Author Closing Comment

by:koossa
ID: 39159113
Thank you
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

688 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