删除#tempTable中ID的行

时间:2014-10-09 22:55:18

标签: sql-server sql-server-2008 tsql

为什么我不能这样做:

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

2 个答案:

答案 0 :(得分:1)

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

试试上面的

答案 1 :(得分:1)

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