触发器 (AFTER/BEFORE) 在 Mysql
Trigger (AFTER/BEFORE) In Mysql
我有一个工作正常的触发器。
CREATE TRIGGER crm_listings__au
AFTER UPDATE
ON crm_listings FOR EACH ROW
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), NULL, d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
现在我还想跟踪字段列名。我想我不能在上面的查询中做所以我改为下面的触发器
CREATE TRIGGER crm_listings__au
BEFORE UPDATE
ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'type', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'price', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
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 10
更新:
我在 Whosebug
上关注了这个 post
@kordirko: Can you please explain a little bit?
请研究文档:http://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
If you use the mysql client program to define a stored program
containing semicolon characters, a problem arises. By default, mysql
itself recognizes the semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire
stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
简单示例 - 我正在使用 MySql Workbench 并复制并粘贴了您的触发器。首先是一些虚拟表:
create table crm_listings(
id int,
type int,
price int
);
create table crm_listings_versions(
ttype varchar(100),
something varchar(100),
d date,
something1 varchar(100),
id int,
type int,
price int
);
现在我 运行 你的代码 没有分隔符
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
Error Code: 1064. 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 10 0.000 sec
结果=错误
然后相同,但使用 DELIMITER 命令:
DELIMITER $$
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
DELIMITER ;
0 row(s) affected
结果=成功
我有一个工作正常的触发器。
CREATE TRIGGER crm_listings__au
AFTER UPDATE
ON crm_listings FOR EACH ROW
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), NULL, d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
现在我还想跟踪字段列名。我想我不能在上面的查询中做所以我改为下面的触发器
CREATE TRIGGER crm_listings__au
BEFORE UPDATE
ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'type', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'price', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
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 10
更新:
我在 Whosebug
上关注了这个 post@kordirko: Can you please explain a little bit?
请研究文档:http://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
简单示例 - 我正在使用 MySql Workbench 并复制并粘贴了您的触发器。首先是一些虚拟表:
create table crm_listings(
id int,
type int,
price int
);
create table crm_listings_versions(
ttype varchar(100),
something varchar(100),
d date,
something1 varchar(100),
id int,
type int,
price int
);
现在我 运行 你的代码 没有分隔符
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
Error Code: 1064. 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 10 0.000 sec
结果=错误
然后相同,但使用 DELIMITER 命令:
DELIMITER $$
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
DELIMITER ;
0 row(s) affected
结果=成功