具有左连接和分组的SQL Server Update

时间:2010-03-17 17:52:05

标签: sql sql-server tsql sql-update

我正在对我们的数据库进行更新,并希望更新另一个表中没有现有项目的行。我可以一起加入这些表,但是在分组表以获取行数

时遇到了麻烦
UPDATE dpt
SET dpt.active = 0 
FROM DEPARTMENT dpt
LEFT JOIN DOCUMENTS doc on dpt.ID = doc.DepartmentID
GROUP BY dpt.ID
HAVING COUNT(doc.ID) = 0

我该怎么办?

2 个答案:

答案 0 :(得分:5)

使用:

UPDATE DEPARTMENT
   SET active = 0 
 WHERE NOT EXISTS(SELECT NULL 
                    FROM DOCUMENTS doc
                   WHERE doc.departmentid = id)

答案 1 :(得分:4)

UPDATE  department
SET     active = 0
WHERE   id NOT IN
        (
        SELECT  departmentId
        FROM    doc
        )