2014-10-09 75 views
0

ID为什么我不能做到这一点:删除行,其中在#tempTable

declare @myTempTableList TABLE (
CommId int 
) 
insert into @myTempTableList (CommId) 
VALUES 
(742), (803) 

delete from myRealTable where MyRealTableId in (
    select mrt.MyRealTableId from MyRealTable mrt 
    where commId in (@myTempTableList) 
) 

它告诉我,我必须声明标量变量@myTempTableList

回答

1
delete from myRealTable where MyRealTableId in (
    select mrt.MyRealTableId from MyRealTable mrt 
    where commId in (SELECT CommId FROM @myTempTableList) 
) 

试试上面的

1
DELETE FROM myRealTable 
WHERE EXISTS (SELECT 1 
       FROM @myTempTableList 
       WHERE myRealTable.MyRealTableId = @myTempTableList.CommId)