Home > Software design >  Using a case statement within an insert into statement
Using a case statement within an insert into statement

Time:02-03

I try running the below code and I get an error saying

Unexpected CASE

I am not sure what's wrong with the code. My original table already has 5 columns. It has the Customer id, first name, last name, debt, and accountstatus. I have a blank column called "accountstatus" which I need to fill in using a case statement.

INSERT INTO customeraccounts (accountstatus)
    CASE
        WHEN debt = 0 THEN "closed"
        WHEN debt > 0 THEN "open"
        WHEN debt < 0 THEN "refund needed"
    END

CodePudding user response:

To fill the accountstatus using case statement You can do it as follows :

Update customeraccounts
set accountstatus = CASE
  When debt = 0 then "closed"
  When debt > 0 then "open"
  When debt < 0 then "refund needed"
END
  •  Tags:  
  • sql
  • Related