create trigger _before_insert_orders before insert on orders for each row begin declare orders goods id int; declare goods_inventory_number decimal(10,2); declare orders_goods_name varchar(50); declare msg varchar (100); if not exists (select goods name from goods where goods id=new.goods id) then setmsg-’你订购的商品不存在!请重新下订单!! signal sqlstate '45000' set message_text=msg; end if; select inventory_number,goods_name into goods_inventory_number,orders_goods_name from goods where goods_id=new.goods_id; if goods inventory number<new.number then setmsg=concat(orders_goods_name’库存不足,请修改订购商品的数量!”; signal sqlstate '45000' set message text=msg; else update goods set inventory number=inventory number-new.number where goods id=new.goods id; end if;end 检查这段代码哪里有错
时间: 2024-01-17 12:02:43 浏览: 63
import_Oracledb.rar_operation_oracle insert into_sql loader
这段代码有以下几个错误:
1. `declare orders goods id int;` 这行代码有语法错误,应该是 `declare orders_goods_id int;`。
2. `setmsg-’你订购的商品不存在!请重新下订单!! signal sqlstate '45000' set message_text=msg;` 这行代码也有语法错误,`setmsg` 应该改为 `set msg`。
3. `select inventory_number,goods_name into goods_inventory_number,orders_goods_name from goods where goods_id=new.goods_id;` 这行代码应该是 `select inventory_number,goods_name into goods_inventory_number,orders_goods_name from goods where goods_id=new.goods_id;`。
4. `setmsg=concat(orders_goods_name’库存不足,请修改订购商品的数量!”; signal sqlstate '45000' set message text=msg;` 这行代码有语法错误,应该将 `orders_goods_name` 改为 `orders_goods_name + '`,并且将 `message text` 改为 `message_text`。
修改后的代码如下:
```
create trigger _before_insert_orders before insert on orders for each row
begin
declare orders_goods_id int;
declare goods_inventory_number decimal(10,2);
declare orders_goods_name varchar(50);
declare msg varchar (100);
if not exists (select goods_name from goods where goods_id=new.goods_id) then
set msg='你订购的商品不存在!请重新下订单!!';
signal sqlstate '45000' set message_text=msg;
end if;
select inventory_number,goods_name into goods_inventory_number,orders_goods_name from goods where goods_id=new.goods_id;
if goods_inventory_number<new.number then
set msg=concat(orders_goods_name, '库存不足,请修改订购商品的数量!');
signal sqlstate '45000' set message_text=msg;
else
update goods set inventory_number=inventory_number-new.number where goods_id=new.goods_id;
end if;
end;
```
阅读全文