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
 General SQL Server Forums
 New to SQL Server Programming
 How to use multiple keys as primary key

Author  Topic 

velvettiger
Posting Yak Master

115 Posts

Posted - 2008-06-18 : 17:38:23
Hi Guys,

Can someone tell me what is the best way to check 2 tables lets say tb1 and tb2, to find those customers that are in tb1 but not in tb2 using four fields(cardno,spend,date,refno) as the primary key. Duplicates fields are in tb1

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-18 : 18:10:05
SELECT *
FROM Table1 t1
WHERE NOT EXISTS (SELECT * FROM Table2 t2 WHERE t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2 AND ...)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

velvettiger
Posting Yak Master

115 Posts

Posted - 2008-06-18 : 18:37:10
Hi again,

Will this capture duplicate entries as well? For instance if there are 2 rows that are the same will both rows be produced or just the one row?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-18 : 18:40:48
It will show all rows that don't exist in Table2 but are present in Table1. If you don't want to display the dupes, then add a DISTINCT.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -