Solved

t-sql insert into table if not exist

Posted on 2013-05-10
6
979 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

785 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