为什么说这个触发器包含sql语法错误?(Why does it say this trigger contains sql syntax errors?)

编程入门 行业动态 更新时间:2024-10-28 12:30:31
为什么说这个触发器包含sql语法错误?(Why does it say this trigger contains sql syntax errors?)

我已经在mysqlworkbench中编写了一个触发器,它应该在将新行添加到订单表后更新产品库存(我的代码是荷兰语,这就是为什么即时解释这个)但是当我尝试应用它时它不起作用mysqlworkbench说它的sql代码包含错误。

这是触发器:

CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW BEGIN if Product.productnr = NEW.productnr then Update Product Set Hoeveelheid = (Hoeveelheid - NEW.aantal) Where productnr = NEW.productnr; end if; END

i have written a trigger in mysqlworkbench that should update the product stock after a new line had been added to the order table(my code is in dutch so thats why im explaining this) But it doesn't work, when i try to apply it mysqlworkbench says its sql code contains errors.

Here's the trigger:

CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW BEGIN if Product.productnr = NEW.productnr then Update Product Set Hoeveelheid = (Hoeveelheid - NEW.aantal) Where productnr = NEW.productnr; end if; END

最满意答案

if语句中的Product是什么? 毫无疑问,这会产生语法错误,因为它无法识别。

我认为你打算让身体简单:

Update Product p. Set Hoeveelheid = (Hoeveelheid - NEW.aantal) Where p.productnr = NEW.productnr;

if无关紧要。

我希望完整版看起来像:

DELIMITER $$ CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW BEGIN UPDATE Product p SET Hoeveelheid = (Hoeveelheid - NEW.aantal) WHERE p.productnr = NEW.productnr; END $$ DELIMITER ;

What is Product in the if statement? That is, no doubt, generating a syntax error, because it is unrecognized.

I think you intend for the body to be simply:

Update Product p. Set Hoeveelheid = (Hoeveelheid - NEW.aantal) Where p.productnr = NEW.productnr;

The if irrelevant.

I would expect the full version to look something like:

DELIMITER $$ CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW BEGIN UPDATE Product p SET Hoeveelheid = (Hoeveelheid - NEW.aantal) WHERE p.productnr = NEW.productnr; END $$ DELIMITER ;

更多推荐

本文发布于:2023-08-04 02:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   语法错误   sql   errors   trigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!