1

我尝试在 mysql 数据库上执行触发器。命令执行成功,但触发器不起作用。

DELIMITER #

CREATE TRIGGER generate_coupon AFTER INSERT ON order
FOR EACH ROW
BEGIN

DECLARE userid, couponvalue INT;
DECLARE couponcode VARCHAR;
SELECT idUser INTO userid FROM indication WHERE email = NEW.email;
SET couponvalue = 20;
SET couponcode = 'abc123';

INSERT INTO coupon(idUser,idOrder,couponvalue,couponcode) values(userid, NEW.id, couponvalue, couponcode);

END# 

DELIMITER ;
4

1 回答 1

1

我怀疑您的问题是由变量之间的冲突couponvalue以及couponcode表中的同名列引起的coupon。如Local Variable Scope and Resolution中所述:

局部变量不应与表列同名。

您可以将触发器简化为以下内容,这样可以完全避免此问题:

CREATE TRIGGER generate_coupon AFTER INSERT ON order FOR EACH ROW
  INSERT INTO coupon
    (idUser, idOrder, couponvalue, couponcode)
  SELECT idUser, NEW.id, 20, 'abc123'
  FROM   indication
  WHERE  email = NEW.email
;
于 2012-11-26T12:52:27.880 回答