all groups > sql server programming > march 2007 >
You're in the

sql server programming

group:

Inserting only non existing rows


Inserting only non existing rows Ant
3/22/2007 10:02:00 PM
sql server programming: Hi,

I have two tables residing in seperate DB's. I need to copy rows from one to
the other but only rows that don't exist in the first table. It has a
composite pk & must only pull certain table Ids'

This is what I'm doing below:


select * from DB1.dbo.Table1 db1
where Name = 'SomeName'
and not exists (select * from DB2.dbo.Table1 db2
where db1.Key1ID = db2.key1ID
and db1.Key2ID = db2.Key2ID )

But this pulls no rows. I need to pull only the rows that don't exist in the
first DB. How is this done?

Thanks very much for your help on this
Re: Inserting only non existing rows xyb
3/22/2007 10:22:45 PM
On 3=D4=C223=C8=D5, =CF=C2=CE=E71=CA=B102=B7=D6, Ant <A...@discussions.micr=
[quoted text, click to view]

try this:)

insert into DB1.dbo.Table1
select * from DB2.dbo.Table1 db1
where Name =3D 'SomeName'
and not exists (select * from DB1.dbo.Table1 db2
where db1.Key1ID =3D db2.key1ID
and db1.Key2ID =3D db2.Key2ID )
Re: Inserting only non existing rows Ant
3/23/2007 2:13:08 AM
xyb, Thanks! I had the selection criteria coming from the wrong tbl.

Cheers!

Ant

[quoted text, click to view]
AddThis Social Bookmark Button