GLP
GLP

Reputation: 3675

how to combine two update statements

I have two following update statements:

update tb1
set col1=1
where id in ('1', '2', '3')

update tb1
set col1=0
where id not in ('1', '2', '3')

Can I combine above two update statements into one?

Upvotes: 2

Views: 3541

Answers (2)

Peter O'Toule
Peter O'Toule

Reputation: 21

UPDATE tb1 
SET col1 = CASE WHEN id in (1, 2, 3) 
                    THEN 1 
                WHEN id NOT IN (1, 2, 3)
                    THEN 0
                ELSE
                    col1
           END ;

Upvotes: 2

András Ottó
András Ottó

Reputation: 7695

Try this:

UPDATE tb1 SET col1= CASE WHEN id in ('1', '2', '3') THEN 1 ELSE 0 END

Upvotes: 6

Related Questions