在此触发器中:
CREATE TRIGGER `after_insert_normas_revision`
AFTER INSERT ON `normas_revision` FOR EACH ROW
BEGIN
DECLARE VAR_P_RHID INT;
DECLARE VAR_P_VERSION INT;
SELECT MAX(a.`rhID`)
INTO VAR_P_RHID
FROM `revision_history` a
WHERE a.nID = new.`nID`;
IF VAR_P_RHID IS NULL THEN
SET VAR_P_VERSION = 0;
ELSE
SELECT `version`
INTO VAR_P_VERSION
FROM `revision_history` a
WHERE a.`rhID` = VAR_P_RHID;
END IF;
DECLARE VAR_NEW_VERSION INT;
SET VAR_NEW_VERSION = VAR_P_VERSION + 1;
INSERT INTO `revision_history`
(`revID`, `nID`, `userID`, `timestamp`,
`prev_rhID`, `version`, `action`, `comment`)
VALUES
(new.`revID`, new.`nID`, new.`userID`, CURRENT_TIMESTAMP, VAR_P_RHID, VAR_NEW_VERSION, 'UPDATE', new.`_rev_comment`);
END
我收到此错误
Schema Creation Failed:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'DECLARE VAR_NEW_VERSION INT; 附近使用正确的语法;设置 VAR_NEW_VERSION = VAR_P_VERSION + 1;' 在第 23 行:
如果我删除
DECLARE VAR_NEW_VERSION INT;
SET VAR_NEW_VERSION = VAR_P_VERSION + 1
或将局部变量更改为会话变量(以 @ 开头)然后代码运行正常。
问:为什么会这样?