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.

 All Forums
 SQL Server 2005 Forums
 SSIS and Import/Export (2005)
 Import Data minus duplicates

Author  Topic 

paulmoss
Starting Member

14 Posts

Posted - 2008-02-20 : 12:11:10
Hi

I am currently trying to import data from a table in 1 database into a table of the same name in another database. This in it's self is simple, however to add a twist to the proceedings there is data that exists in both tables. I just want to import the data that doesn't exist in the table I am importing into.

Please can you advise as to the best method to use

Many thanks

Paul

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-02-20 : 12:19:31
declare @source table (c1 int, c2 int, primary key (c1))
declare @destination table (c1 int, c2 int, primary key (c1))

insert into @source select 1,1 union select 2, 2 union select 3,3
insert into @destination select 1,1 union select 2, 2

select * from @source
select * from @destination

insert into @destination (c1,c2)
select s.c1,s.c2
from @source s left join @destination d on s.c1 = d.c1
WHERE d.c1 is null

select * from @source
select * from @destination



"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

paulmoss
Starting Member

14 Posts

Posted - 2008-02-20 : 12:27:02
Hi

Thanks for the response.

Could you please break down the components of the script, and explain what each bit is
i.e
@source = source database
@destination = destination database


Many thanks

Paul
Go to Top of Page

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-02-20 : 12:35:20
The script is just a working example.
The part you need is

insert into @destination (c1,c2)
select s.c1,s.c2
from @source s left join @destination d on s.c1 = d.c1
WHERE d.c1 is null

And it is joining the source and destination tables on the primary key and only selecting records that do not exist in the destination
IE WHERE d.c1 is null


"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-02-20 : 15:38:35
Does the table have a key?

What if data in the destination table for a particular key has different data on the row..you want to update it

What if it's missing in the source, do you want to delete it in the destination table?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

dineshasanka
Yak Posting Veteran

72 Posts

Posted - 2008-02-20 : 16:25:38
If you have key in your table, you can use a lookup and if set the lookup to re-direct in a filure.
then set the failure path to insert data
Go to Top of Page
   

- Advertisement -