CREATE TRIGGER `write_in` BEFORE INSERT ON `order_detail` FOR EACH ROW begin declare ainventory int; select inventory into ainventory from commodity where com_no=new.com_no; if new.order_num >= ainventory then signal sqlstate '45000' set message_text='商品库存不足'; end if; end;;哪里有语法错误
时间: 2024-02-14 22:13:40 浏览: 63
在这个触发器代码中,语法错误在于信号量(signal)语句的最后缺少分号。正确的代码如下所示:
```
CREATE TRIGGER `write_in` BEFORE INSERT ON `order_detail`
FOR EACH ROW
BEGIN
DECLARE ainventory INT;
SELECT inventory INTO ainventory FROM commodity WHERE com_no = new.com_no;
IF new.order_num >= ainventory THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '商品库存不足';
END IF;
END;
```
注意到信号量语句的最后有一个分号,这是必须的。另外,在此代码段中,DECLARE语句中的变量名和类型之间应该有一个空格。这些修正后,该触发器应该能够正常工作了。
阅读全文