简体   繁体   中英

What is wrong with this trigger syntax?

Look at this code:

DELIMITER $$
DROP TRIGGER IF EXISTS `after_product_insert` $$
CREATE TRIGGER `after_product_insert`
AFTER INSERT ON `products`
    FOR EACH ROW
    UPDATE `categories` SET product_count=product_count+1 where id = NEW.category_id;
END$$

This runs ok but shows some error. Here is the output:

Query OK, 0 rows affected (0.16 sec)

ERROR 1064 (42000): 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 'END' at line 1

So, even though the trigger is created - the script (containing this triggers) dies because of error. This might be really simple but somehow I am missing the point. Help anyone?

It is a two table trigger, so id is probably ambiguous. Add the table name to it. It wouldn't hurt to add the table name to every field reference to be crystal clear.

Never mind that. There is no BEGIN before the UPDATE . The FOR statement seems to expect BEGIN and END around the statements to execute.

DELIMITER $$
DROP TRIGGER IF EXISTS `after_product_insert` $$
CREATE TRIGGER `after_product_insert`
AFTER INSERT ON `products`
  FOR EACH ROW
  BEGIN
    UPDATE `categories`
      SET product_count=product_count+1
      WHERE id = NEW.category_id;
  END$$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM