007
007

Reputation: 2186

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

Upvotes: 0

Views: 471

Answers (1)

John Woo
John Woo

Reputation: 263883

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'

Upvotes: 2

Related Questions