0

I have a trigger which is working fine.

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;

Now I want to keep track of the field column name also. I am thinking I could not do in above query so I changed to below trigger

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;
$$

When I run this code, I get this error:

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

UPDATE:

I followed this post on stackoverflow

4

1 回答 1

0

@kordirko:你能解释一下吗?

请研究文档:http ://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html

如果使用mysql客户端程序定义包含分号字符的存储程序,就会出现问题。默认情况下,mysql 本身将分号识别为语句分隔符,因此必须临时重新定义分隔符,以使 mysql 将整个存储的程序定义传递给服务器。

要重新定义 mysql 分隔符,请使用 delimiter 命令。下面的示例显示了如何为刚刚显示的 dorepeat() 过程执行此操作。分隔符更改为 // 以使整个定义能够作为单个语句传递给服务器,然后恢复为 ; 在调用程序之前。这使 ; 过程主体中使用的分隔符将传递给服务器,而不是由 mysql 本身解释。


简单示例 - 我正在使用 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
);

现在我在没有 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;
$$

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

结果=成功

于 2016-03-20T09:43:08.963 回答