2016-03-20 15 views
0

私は正常に動作しているトリガーがあります。トリガー(AFTER/BEFORE)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; 

ここでフィールドの列名も追跡したいと思います。私はこれを踏襲し

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:私は、私はこのコードを実行すると、私は上記のクエリで私はトリガ

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

以下に変更することができなかった、私はこのエラーを取得する考えていますstackoverflow

+0

唯一のことは、あなたが ' 'type''にnull'なので'から4列目を変更したことである - 多分4列目は、 'varchar'の列ではありません? – radoh

+0

'create trigger'コマンドに' DELIMITER $$ 'の前に忘れてしまいました。また、このコマンドの後に '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 $$ 
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