gpt4 book ai didi

mysql - 创建Mysql触发器的语法

转载 作者:行者123 更新时间:2023-11-30 00:24:03 26 4
gpt4 key购买 nike

CREATE TRIGGER expiryset  ON test FOR INSERT
BEGIN
DECLARE exp TIMESTAMP;
DECLARE name TEXT;
SET @name := (SELECT name from test ORDER BY created_at DESC limit 1);
SET @exp := (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP)));
UPDATE test SET expiry = @exp WHERE name= @name;
END;

上述触发器有效,但仅在执行时我无法保存它。语法有什么问题

最佳答案

您必须将 DELIMITER 设置为与默认 ; 不同的值,才能将整个代码放在一起。

DELIMITER $$

CREATE TRIGGER expiryset AFTER INSERT ON test FOR EACH ROW
BEGIN
DECLARE exp TIMESTAMP;
DECLARE name TEXT;
SET @name := (SELECT name from test ORDER BY created_at DESC limit 1);
SET @exp := (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP)));
UPDATE test SET expiry = @exp WHERE name= @name;
END$$

DELIMITER ;

关于mysql - 创建Mysql触发器的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com