插入前创建 mysql 触发器(语法错误)
Create mysql trigger before insert (Syntax error)
您好,我正在创建一些触发器,以便在像这样插入行之前更新插入查询中的值
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
但是我收到了这样的错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
我有点不明白第 4 行的错误是什么。请有人解释一下。谢谢
根据使用的client/ide,您可能需要设置分隔符,例如
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
请查看https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html
您好,我正在创建一些触发器,以便在像这样插入行之前更新插入查询中的值
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
但是我收到了这样的错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
我有点不明白第 4 行的错误是什么。请有人解释一下。谢谢
根据使用的client/ide,您可能需要设置分隔符,例如
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
请查看https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html