我写了一个触发器。但是在使用case语句时遇到错误

时间:2018-02-15 06:24:33

标签: mysql

触发:

DELIMITER $$

DROP TRIGGER /*!50032 IF EXISTS */ `customersh`$$

CREATE
    TRIGGER `customersh` AFTER UPDATE ON `customers` 
    FOR EACH ROW BEGIN

IF old.customer_status_id !=new.customer_status_id  THEN
    INSERT INTO customers_h (
    status_changed_date,
    status_changed_by,
    loan_request_initial_id,
    verification_approval_id,
    ) VALUES
    ( 
    old.status_changed_date,
    old.status_changed_by,
    (CASE WHEN old.customer_status_id IN (2,3,4) AND new.customer_status_id IN (5,7,3,4)  
    THEN SELECT MAX(loan_request_initial_id) FROM loan_request_initial 
    WHERE customer_id = old.customer_id ELSE NULL END),

    old.verification_approval_id,
    );
END IF;
    END;
$$
DELIMITER ;

错误:

  

错误代码:1064

     

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第106行的“SELECT MAX(loan_request_initial_id)FROM loan_request_initial”附近使用正确的语法

请帮我解决此错误。案例陈述可能是错误的。

1 个答案:

答案 0 :(得分:0)

正确检查括号

CASE WHEN old.customer_status_id IN (2, 3, 4)
  AND new.customer_status_id IN (5, 7, 3, 4) THEN (
    SELECT
      MAX(loan_request_initial_id)
    FROM
      loan_request_initial
    WHERE
      customer_id = old.customer_id
  ) ELSE NULL END