触发器创建Mysql中的语法

时间:2014-04-11 19:24:16

标签: mysql sql database odbc

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;

上述触发器有效,但只有在执行时我无法保存。语法错误

1 个答案:

答案 0 :(得分:0)

您必须将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 ;