问题描述:

I have the following table

I want to update the status from 0 to 1 and 1 to 0 in a single query

 ID Name Status

------------------------------------------

1 Ben 0

2 John 1

3 Mark 0

4 Gates 0

5 Ricky 1

网友答案:

If column status can be null or status can have more than two values, this solution should work for you.

update tab
   set status = case 
                  when status=0 then 1
                  when status=1 then 0
                end 
网友答案:

Plenty of ways to do this, as the number of answers already show. Mine would just be:

UPDATE [Table] set status = 1-status

If there are non-0 and non-1 rows in the table that shouldn't be touched, I'd also add:

WHERE status in (0,1)
网友答案:

If you have only two values (e.g 0 and 1) try this:

UPDATE MyTable
SET Status = CASE WHEN Status = 0 THEN 1 ELSE 0 END
相关阅读:
Top