简体   繁体   中英

UPDATE multiple Columns in SSMS/T-SQL:

I have the following scenario.

 UPDATE ATable
SET C1 = 'XValue'
WHERE C2 = 'YValue'

UPDATE ATable
SET C3 = 'MValue'
WHERE C4 = 'NValue'

For performance tuning, can I do anything to make them run under one UPDATE? Would it be better?

Thank you

You can run one update using CASE

UPDATE  Atable
SET     C1 = CASE WHEN c2 = 'yValue' THEN 'xValue' ELSE c1 END,
        C3 = CASE WHEN c4 = 'nvalue' THEN 'mValue' ELSE c3 END
WHERE   c2 = 'yValue' OR c4 = 'nvalue'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM