我在触发时有sql错误语法

bd1hkmkf  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(238)

我想创建一个触发器 id_pembayaran 在pembayaran表上,但是有一个sql语法错误

DELIMITER $$
CREATE TRIGGER id_bayar 
    BEFORE INSERT ON pembayaran
    FOR EACH ROW
    BEGIN
    SELECT COUNT(*) FROM pembayaran INTO @idpembayaran;
    SELECT id_jenis_pembayaran, IF(id_jenis_pembayaran = 'DP', 
       SET new.id_pembayaran = CONCAT('DP','-',   DATE_FORMAT(curdate(),'%Y%m%d'),'-',@idpembayaran+1),
       SET new.id_pembayaran = CONCAT('LNS','-', DATE_FORMAT(curdate(),'%Y%m%d'),'-',@idpembayaran+1)
      ) FROM pembayaran;
END$$

DELIMITER ;
yh2wf1be

yh2wf1be1#

我认为您打算在每一行中设置值:

DELIMITER $$
CREATE TRIGGER id_bayar 
    BEFORE INSERT ON pembayaran
    FOR EACH ROW
BEGIN
    SET new.id_pembayaran =
        (SELECT (CASE WHEN new.id_jenis_pembayaran = 'DP'
                      THEN CONCAT('DP','-',   DATE_FORMAT(curdate(), '%Y%m%d'), '-', COUNT(*) + 1)
                      ELSE CONCAT('LNS','-', DATE_FORMAT(curdate(), '%Y%m%d'), '-', COUNT(*) + 1)
                 END)
         FROM pembayaran
        );
END$$

DELIMITER ;

相关问题