Connecting Tech Pros Worldwide Forums | Help | Site Map

Sql update command question

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: Oct 19 '09
Hi,

I want to know are there any ways to do the statement below in one SQL command?

update [table_a] set [column_a] = 'A' where id in (1,2,3)
update [table_a] set [column_a] = 'B' where id in (4,5,6)

(above statement use 2 times for updating)

PS. thanks for advise if it possible to do :D

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Oct 19 '09

re: Sql update command question


Yes, use the CASE statement.

Expand|Select|Wrap|Line Numbers
  1.  
  2. update [table_a] 
  3. set [column_a] = 
  4. case when id in (1,2,3) then 'A' when id in (4,5,6) then 'B' else NULL end
  5.  
  6.  
Happy Coding!!!

--- CK
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: Oct 20 '09

re: Sql update command question


It's works ! thank you very much

however It's modified all records in the table, if i'd like to modify only in the set of ids, is it possible?
Newbie
 
Join Date: Oct 2009
Posts: 3
#4: Oct 20 '09

re: Sql update command question


I got the solution now, it's just add the 'where' clause after end case :D
Reply