2016-09-15 178 views
-1

我想從#temp_scoring中刪除匹配的帳戶(匹配ID和acct_num),但最終刪除表#temp_scoring中的所有行。下面是代碼:刪除SQL Server中兩個表之間的交集

delete from #temp_scoring 
where exists (
select * from #temp_edu te 
where te.ID = ID 
and te.acct_num = acct_num) 

作爲一個方面說明,我已經創建了一個別名#temp_scoring但它給我一個語法錯誤,當我做到了。

+0

其中上#temp_scoring符合條件的刪除?將此表加入DELETE上想要的表中 – techspider

回答

1
DELETE s 
FROM 
    #temp_scoring s 
    INNER JOIN #temp_edu te 
    ON s.ID = te.ID 
    AND s.acct_num = te.acct_num 

你可以做與聯接

0

你的問題是列名:

delete from #temp_scoring 
where exists (
select * from #temp_edu te 
where te.ID = ID --- Here ID means te.ID 
and te.acct_num = acct_num -- and acct_num means te.acct_num 
) 

需要明確的是,ID = te.ID因爲那是ID最近的封閉範圍。

你可能想這

delete ts 
from #temp_scoring ts 
where exists (
select 1 
from #temp_edu te 
where te.ID = ts.ID 
and te.acct_num = ts.acct_num 
)