[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Inserting only non existing rows

Ant

3/23/2007 5:02:00 AM

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
Ant
2 Answers

xyb

3/23/2007 5:23:00 AM

0

On 3?23?, ??1?02?, Ant <A...@discussions.microsoft.com> wrote:
> 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
> Ant

try this:)

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

Ant

3/23/2007 9:13:00 AM

0

xyb, Thanks! I had the selection criteria coming from the wrong tbl.

Cheers!

Ant

"xyb" wrote:

> On 3æ??23æ?¥, ä¸?å?1æ?¶02å??, Ant <A...@discussions.microsoft.com> wrote:
> > 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
> > Ant
>
> try this:)
>
> insert into DB1.dbo.Table1
> select * from DB2.dbo.Table1 db1
> where Name = 'SomeName'
> and not exists (select * from DB1.dbo.Table1 db2
> where db1.Key1ID = db2.key1ID
> and db1.Key2ID = db2.Key2ID )
>
>