使用CASE WHEN的SQL Update查询

时间:2014-01-16 08:35:31

标签: sql

UPDATE Table_Main SET filtered = CASE 
WHEN (subTable.name='MI'
 AND subTable.id= Table_Main."CTYPE")  
THEN false  ELSE true END
from subTable

该查询仅适用于第一行subTable

2 个答案:

答案 0 :(得分:0)

听起来像MERGE语句的工作(我认为自SQL Server 2008以来可用)!

MERGE Table_Main AS Target
USING subTable as Source
ON (Source.name = 'MI' AND Source.id = Target."CTYPE")
WHEN MATCHED
    THEN UPDATE SET T.filtered = false
WHEN NOT MATCHED BY SOURCE
    THEN UPDATE SET T.filtered = true
OUTPUT $action, Inserted.*, Deleted.*;

答案 1 :(得分:0)

UPDATE Table_Main SET filtered = CASE WHEN (subTable.name='CS') 
THEN false ELSE true END 
FROM subTable 
WHERE subTable.id = mainTable."CTYPE"