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
 Transact-SQL (2005)
 Comparing a colum in two tables

Author  Topic 

CSharpNewbie
Starting Member

39 Posts

Posted - 2009-07-06 : 22:57:28
Hi, I am creating a stored proc that compares the ID column in table A, with the ID column in table B, and then adds any missing records in table B to make both tables the same.

I need help with the query.

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-06 : 23:00:27
[code]
insert into tableb (ID)
select a.ID
from tablea a
where not exists (select * from tableb x where x.ID = a.ID)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-08 : 12:49:31
also

insert into tableb (ID)
select a.ID
from tablea a
where a.ID not in (select x.ID from tableb x )

insert into tableb (ID)
select a.ID
from tablea a
left join tableb x
on a.ID =x.ID
where x.ID is null
Go to Top of Page
   

- Advertisement -