Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

t-sql insert into table if not exist

Posted on 2013-05-10
6
Medium Priority
?
1,042 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 1000 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

610 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