2012-04-06 72 views
2
CREATE TRIGGER google_inventory BEFORE UPDATE ON `cataloginventory_stock_item` 
FOR EACH ROW 
BEGIN 
    update `catalog_product_entity_int` 
    set value=20 
    where attribute_id=552 
      and entity_id=NEW.product_id 
      and NEW.is_in_stock=0; 
    update `catalog_product_entity_int` 
    set value=21 
    where attribute_id=552 
      and entity_id=NEW.product_id 
      and NEW.is_in_stock=1; 
    END; 

它給了我下面的錯誤:有什麼不對這個MySQL創建觸發器聲明

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 4

我的職業是SQL Server的傢伙,不能把我的手指上的問題是什麼。

+0

是什麼NEW.product_id和NEW.is_in_stock?是否來自另一個表,如果是這樣,那麼你需要加入它 – Taryn 2012-04-06 21:23:53

+0

新的觸發器語法是指根據此文檔在表中進行的更改http://dev.mysql.com/doc/refman /5.0/en/create-trigger.html – RThomas 2012-04-06 21:24:47

回答

5

您需要更改分隔符。沒有它,分號結束CREATE TRIGGER語句。

delimiter | 

CREATE TRIGGER google_inventory BEFORE UPDATE ON `cataloginventory_stock_item` 
FOR EACH ROW 
BEGIN 
    update `catalog_product_entity_int` 
    set value=20 
    where attribute_id=552 
      and entity_id=NEW.product_id and NEW.is_in_stock=0; 
    update `catalog_product_entity_int` 
    set value=21 
    where attribute_id=552 
      and entity_id=NEW.product_id and NEW.is_in_stock=1; 
    END; 
| 

delimiter ; 
+0

很高興我問過......我從來沒有想到在我自己有限的mysql體驗上。謝謝。 – RThomas 2012-04-06 21:29:35

+0

我很高興我可以幫助:) – 2012-04-06 21:35:56

3

觸發器中的分號正在搞砸了。您必須使用DELIMITER關鍵字來更改MySQL正在使用的分隔符。

http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

mysql> delimiter // 

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT) 
    -> BEGIN 
    -> SELECT COUNT(*) INTO param1 FROM t; 
    -> END// 
Query OK, 0 rows affected (0.00 sec) 

mysql> delimiter ;