question

Gogolo avatar image
Gogolo asked

Simple update

Dears, how to update several column with different where condition ?? sample query bellow: **update tbl set field1 = '12' where field3 = '1' and set fiel1 = '13' where field3 = 2**
update
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Matt Whitfield avatar image
Matt Whitfield answered
That would be two separate update statements. If you really want to do it in one, then you could get funky using case... UPDATE tbl SET field1 = CASE WHEN condition THEN new_value ELSE field1 END, field2 = CASE WHEN condition THEN new_value ELSE field2 END
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
You need to change your case statement to do all the logic for that field. You can have multiple WHEN .... THEN ... sections in a CASE condition: CASE WHEN condition1 THEN new_value1 WHEN condition2 THEN new_value2 ELSE field1 END
1 Like 1 ·
Gogolo avatar image Gogolo commented ·
Msg 264, Level 16, State 1, Line 1 The column name 'field1' is specified more than once in the SET clause. A column cannot be assigned more than one value in the same SET clause. Modify the SET clause to make sure that a column is updated only once. If the SET clause updates columns of a view, then the column name 'field1' may appear twice in the view definition.
0 Likes 0 ·
Gogolo avatar image Gogolo commented ·
I cannot update twice or more times, same column in one set clause.
0 Likes 0 ·
Gogolo avatar image Gogolo commented ·
Thank you very much!!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.