Will Will - 1 year ago 66
SQL Question

Updating multiple values within the one column

I have the following SQL which I have written to update a tables values:

Update Table
SET S_Type = 'Versus'
Where S_Type = 'REGULAR'
SET S_Type = 'Free'
Where S_Type = 'CASH';

My SQL is rather rusty, and my colleague told me something was up with it but didn't tell me what!

The only thing that comes to mind is I have not referred to the Table.Column in the set and Where code.

Is there any issue updating a column as such? What is the best practice when updating a column for multiple values?


Answer Source

Here , we are using case statement and find result like as where clause :

update tablename
set S_Type = (case S_Type  when 'REGULAR' then 'Versus'
                           when 'CASH' then 'free' 
                           else s_type 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download