2016-03-20 2 views
0

У меня есть триггер, который работает нормально.Триггер (ПОСЛЕ/ПЕРЕД) В 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

UPDATE:

Я следовал за этим разместить на stackoverflow

+0

Единственное, что приходит на ум, что вы изменили 4-й столбец из 'null' в' 'type'' - может быть, четвёртая колонка не 'varchar' колонка ? – radoh

+0

Вы забыли добавить 'DELIMITER $$' в команду' create trigger'. Также не забудьте добавить «DELIMITER;» после этой команды. – krokodilko

+0

@kordirko: Не могли бы вы немного объяснить? – DOE

ответ

0

@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 
); 

А теперь я запускаю свой код без 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 

Результат = Успех

+0

О, да, я нашел ... разделитель // – DOE