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 2000 Forums
 SQL Server Development (2000)
 EXISTS Function

Author  Topic 

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-09-22 : 16:22:06
I need to write a stored procedure that would compare 2 tables by taking the first table and copying all of the records into the second table that do not exist there. For example...

Table A
Person 1
Person 2
Person 3

Table B
Person 1

After the stored procedure ...

Table B
Person 1
Person 2
Person 3

Any records in A that are not in B are inserted into B.

I've used the EXISTS function for things similar this to delete and update, but i'm not sure how it works for inserting.

Thanks.

ronstone
Starting Member

32 Posts

Posted - 2005-09-22 : 16:32:20
I think you would use left join with your insert where the columns in table b you are comparing are null
Go to Top of Page

ronstone
Starting Member

32 Posts

Posted - 2005-09-22 : 16:35:58
INSERT INTO TableB
SELECT TableA.Name
FROM TableA
LEFT JOIN TableB ON (TableA.Name = TableB.Name)
WHERE TableB.Name IS NULL
Go to Top of Page
   

- Advertisement -