views:

268

answers:

2

I'm making an update to our datbase and would like to update rows that do not have existing items in another table. I can join the tables together, but am having trouble 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 be doing?

+2  A: 
UPDATE  department
SET     active = 0
WHERE   id NOT IN
        (
        SELECT  departmentId
        FROM    doc
        )
Quassnoi
+1: Beat me by 12 seconds, sheesh...
OMG Ponies
+2  A: 

Use:

UPDATE DEPARTMENT
   SET active = 0 
 WHERE NOT EXISTS(SELECT NULL 
                    FROM DOCUMENTS doc
                   WHERE doc.departmentid = id)
OMG Ponies
Gracias Senior...
Marty Trenouth
@Marty Trenouth: de nada
OMG Ponies
I'm pretty sure it should be Señor...unless you were trying to insinuate that OMG Ponies is old! LOL
Leslie