update with a case statement

  •  

    You can use the following Query :

     

    update test_update

    set name=(case when id=1 then 'aaa'

                           when id=2 then 'bbb'

                            when id=3 then 'ccc'

                    else null end)

    where id=?

     

  • And change the id = ? to id in (1, 2, 3)

  • Thanks for your help. It helped me a lot.

  • Alternative:

    UPDATE t

    set t.Name = u.name

    from test_update t

    inner join

    (

      select 1 as Id, 'aaa' as Name union all

      select 2, 'bbb' union all

      select 3, 'ccc'

    )

    u on t.Id = u.Id

Viewing 4 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply