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 2008 Forums
 Transact-SQL (2008)
 moving unique records between tables

Author  Topic 

Peter Smith
Starting Member

8 Posts

Posted - 2009-12-16 : 15:42:18
How can I, in a single query, do the following:

I have 2 tables, tbSource & tbTarget, which both have the same columns but maybe various records.

I want to check if a record from tbSource already occurs in tbTarget. A record occurs in tbTarget if the columns "name" AND "zipcode" are the same in both tables, the rest of the columns do not matter.
If the record occurs, do nothing. If the record DOENST occur, then insert the record and all its columns from tbSource into tbTarget (and preferably delete the record from tbSource)

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-12-16 : 15:52:26
Use a left join.

insert into target
(column1, column2,column3...)
Select A.*
From tblSource A
left join TblTarget B on A.name = B.name and A.zip = B.zip
where B.name is null

An infinite universe is the ultimate cartesian product.
Go to Top of Page
   

- Advertisement -