简体   繁体   中英

Invalid syntax CASE statement

I've been struggling all day with a really simple case statement in SQL...

I have this piece of code:

UPDATE `alugueis`
    SET    `status` = CASE
             WHEN `pago_em` is null THEN 'Atrasado'
             WHEN `pago_em` is '' THEN 'Atrasado'
             ELSE `status`
           END
WHERE  `vencimento` <= CURDATE()

and the error I get is: [1064][42000]: (conn:235) You have an error in your SQL syntax; check the manual .......

I don`t think I am mixing up the two forms of CASE, or am I?

Thank you so much!

I think you intend:

UPDATE alugueis
    SET status = (CASE WHEN pago_em is null THEN 'Atrasado'
                       WHEN pago_em = '' THEN 'Atrasado'
                       ELSE status
                  END)
    WHERE vencimento <= CURDATE();

The problem is the is '' .

This would more normally be written without the CASE :

UPDATE alugueis
    SET status = 'Atrasado'
    WHERE vencimento <= CURDATE() AND
          (pago_em IS NULL OR pago_em = '')

You cannot use IS ''

UPDATE `alugueis`
    SET    `status` = CASE
             WHEN `pago_em` is null THEN 'Atrasado'
             WHEN `pago_em` = '' THEN 'Atrasado'
             ELSE `status`
           END
WHERE  `vencimento` <= CURDATE()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM