如何在mysql中添加触发事件?

时间:2019-11-07 06:29:02

标签: mysql triggers

我有触发器语句,但不起作用

CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
 FOR EACH ROW BEGIN
  INSERT INTO table3_seq VALUES (NULL);
  SET NEW.option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0'));
  

但是它显示错误,例如您的SQL语法错误: check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

1 个答案:

答案 0 :(得分:0)

我假设您要在插入完成后尝试更新序列。看到这个(http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/)。

以下代码段可能有效。

CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
  FOR EACH ROW
  BEGIN
    INSERT INTO table3_seq SET option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0');
  END;

如果它是单个语句,请删除第3行的分号并添加end。同样,以上代码仅在表“ table3_seq”具有一列的情况下有效。