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)
 t1-t2

Author  Topic 

jeff06
Posting Yak Master

166 Posts

Posted - 2007-01-22 : 09:38:30
t2 is a subset of t1, I want t1-t2.
but following code does not seem to work.


SELECT *
FROM t1
except
select *
from t2

How to fix this problem? Thanks.

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2007-01-22 : 09:40:54
SELECT t1.*
FROM t1
LEFT JOIN t2
On t1.keyField = t2.keyField
WHERE t2.keyField is NULL
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-22 : 09:41:52
is t1 & t2 has the same structure ?

else specify the column name


select col1, col2 from t1
except
select cola, colb from t2



KH

Go to Top of Page

jeff06
Posting Yak Master

166 Posts

Posted - 2007-01-22 : 09:48:20
Yes t1 and t2 has same structure
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-22 : 09:52:30
any error message ?


KH

Go to Top of Page

jeff06
Posting Yak Master

166 Posts

Posted - 2007-01-22 : 11:42:42
russell's method works.
Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-22 : 11:46:40
are you not using SQL Server 2005, as the forum name hint?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2007-01-22 : 12:37:45
there is no significant difference between the two. a look at exec plan will show similar cost and same steps, i/o, cpu record counts.

both plans will show a merge join, though left join will show "left join" and except will show "left anti semi join"

at least this is true on the few trials i have run
Go to Top of Page
   

- Advertisement -