如何在mysql触发器中声明变量

时间:2015-05-29 15:50:37

标签: mysql variables triggers

这是我的触发器

CREATE TRIGGER proximo_pago 
AFTER INSERT ON pago FOR EACH ROW
BEGIN
    DECLARE max_orden integer;
    DECLARE num_lote =NEW.lote;
        BEGIN
            SET max_orden = (SELECT MAX(orden) FROM PAGO WHERE LOTE=num_lote);
            SELECT max_orden INTO : NEW.orden from dual;
            END
END

和错误

错误 SQL查询:

CREATE TRIGGER proximo_pago 
AFTER INSERT ON pago FOR EACH ROW
BEGIN
    DECLARE max_orden integer;

MySQL说:文档

  

#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。在第4行

1 个答案:

答案 0 :(得分:0)

 DECLARE TEMPKODE VARCHAR(10);
 DECLARE TEMP VARCHAR(5);

或者如果你想分配你可以使用

 DECLARE TEMP INT DEFAULT 6;

分配变量

myvariable := TEMPKODE;
SET myvariable := TEMPKODE;

也许你的意思是:

CREATE TRIGGER proximo_pago 
AFTER INSERT ON pago FOR EACH ROW
BEGIN
DECLARE max_orden INT;
DECLARE num_lote INT;
    BEGIN
        SET num_lote := NEW.lote;
        SELECT MAX(ordern) INTO max_ordern FROM PAGO WHERE LOTE = num_lote;
        SET NEW.ordern := max_ordern;

END