Update table in a SQL database

时间:2017-08-13 13:44:42

标签: sql oracle sql-update

I am trying to add a column in my table which maps some values in another column to the new column. (Population is another column that I have) This is what I have been using:

UPDATE my_table 
SET popmatch2 
    CASE 
       WHEN popmatch IN ('MFNR', 'GQNR', 'EWNR', 'MHNR', 'OUTONR', 'OUTADVNR') 
          THEN popmatch2 IN ('GENERAL', 'GENERAL', 'GENERAL', 'GENERAL', 'ENDEMIC', 'ENDEMIC') 
          ELSE POPULATION;

This is not working

2 个答案:

答案 0 :(得分:2)

The major problems with your current syntax is that you need to assign the popmatach2 column to something. In this instance, assigning to a CASE expression might make sense. A CASE expression has to return a single value, not group of values, and each WHEN condition should return a single value.

UPDATE my_table
SET popmatch2 = CASE WHEN popmatch IN ('MFNR', 'GQNR', 'EWNR') THEN 'GENERAL'
                     WHEN popmatch IN ('OUTONR', 'OUTADVNR')   THEN 'ENDEMIC'
                     ELSE 'POPULATION' END;

You need to put single quotes around POPULATION if you intend for that to be a string literal.

答案 1 :(得分:1)

This is the correct syntax:

update my_table 
   set popmatch2 = (case when popmatch in ('MFNR', 'GQNR', 'EWNR', 'MHNR')
                         then 'GENERAL'
                         when popmatch in ('OUTONR', 'OUTADVNR') 
                         then 'ENDEMIC'
                         else 'POPULATION'  -- I'm guessing this is really a string
                    end);

You probably want to do a commit after the update, so the changes are committed.

Notes:

  • The case must have an end.
  • The then should be a single string.
  • Each when should result in a single string being returned.