SQL Server update with left connection and group,

I am doing an update for our database and want to update rows that do not have existing elements in another table. I can join the tables together, but I had problems with grouping the table to get a count of the number of rows

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 

What should I do?

+4
source share
2 answers

Using:

 UPDATE DEPARTMENT SET active = 0 WHERE NOT EXISTS(SELECT NULL FROM DOCUMENTS doc WHERE doc.departmentid = id) 
+5
source
 UPDATE department SET active = 0 WHERE id NOT IN ( SELECT departmentId FROM doc ) 
+4
source

Source: https://habr.com/ru/post/1304385/


All Articles