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.
Author |
Topic |
paulmoss
Starting Member
14 Posts |
Posted - 2008-02-20 : 12:11:10
|
HiI 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 useMany thanksPaul |
|
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,3insert into @destination select 1,1 union select 2, 2select * from @sourceselect * from @destinationinsert into @destination (c1,c2)select s.c1,s.c2from @source s left join @destination d on s.c1 = d.c1WHERE d.c1 is nullselect * from @sourceselect * 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 |
 |
|
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 isi.e @source = source database@destination = destination databaseMany thanksPaul |
 |
|
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.c2from @source s left join @destination d on s.c1 = d.c1WHERE d.c1 is nullAnd 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 |
 |
|
X002548
Not Just a Number
15586 Posts |
|
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 |
 |
|
|
|
|
|
|