Search code examples
sqlsql-serversql-update

SQL Server - UPDATE table where ID is in SELECT?


I have a table where I want to update all rows with the ID that exists in the select result.

My pseudo-code:

UPDATE mytable as t
   SET t.status = 'PM'
 WHERE t.ID EXISTS IN (select ID from ...)

I have managed to do the select statement, now I want to use the result of the select statement to update a table.


Solution

  • If you remove the exists you have a valid query from what I can tell.

    UPDATE mytable 
       SET status = 'PM'
     WHERE id IN (select ID from ...)
    

    Works for me in MySql 5.5, not sure which database you're using.