Solved

t-sql insert into table if not exist

Posted on 2013-05-10
6
990 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

840 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