Search code examples
sqlsql-serversql-server-2008

Update single column with multiple values


I am trying to add multiple values at once in existing table using query

That column with NULL values "County" I want it populated with countries like Kenya, Uganda, Cameroon, Ghana.

I have successfully added only one county 'Tanzania' but I couldn't add all of them at once

How to modify

Update Madanga

Set County= 'Tanzania'

Where ID=1

So I can add all countries at once in above query


Solution

  • You know how to set one country:

    update madanga set county = 'Tanzania' where id = 1;
    

    For reasons unknown to us, you want to set more than one country, but not in separate statements like one would expect.

    update madanga set county = 'Tanzania' where id = 1;
    update madanga set county = 'Kenya' where id = 2;
    

    In order to do this in one statement you need a CASE expression:

    update madanga 
    set county = case when id = 1 then 'Tanzania' 
                      when id = 2 then 'Kenya'
                      else county
                 end 
    where id in (1, 2);