Sql update command question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasarus
    New Member
    • Oct 2009
    • 3

    Sql update command question

    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
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Yes, use the CASE statement.

    Code:
    update [table_a] 
    set [column_a] = 
    case when id in (1,2,3) then 'A' when id in (4,5,6) then 'B' else NULL end
    Happy Coding!!!

    --- CK

    Comment

    • Kasarus
      New Member
      • Oct 2009
      • 3

      #3
      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?

      Comment

      • Kasarus
        New Member
        • Oct 2009
        • 3

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

        Comment

        Working...