如何针对复合键为'where in'子句编写sql语句?

时间:2011-01-27 22:51:55

标签: sql-server-2005 composite-key

使用MSSQL 2005我习惯写这样的语句:

delete
from myTable
where ID in (select ID from otherTable where deleted = 1)

如果otherTable有复合主键,我该怎么做?

复合键有两列:

docnum float
version int

(我的google-fu建议使用CTE执行此操作,但我没有经验。)

2 个答案:

答案 0 :(得分:2)

在MS SQL中,您可以这样做:

DELETE T
FROM myTable T
INNER JOIN otherTable OT
  ON T.docnum = OT.docnum
  And T.version = OT.version

也有类似的更新语法。

答案 1 :(得分:1)

您还可以使用存在关键字:

delete t1
from myTable t1
where exists (
    select * from otherTable where docnum = t1.docnum and version = t1.version
)