CREATE TRIGGER Trigger_Product ON Product FOR INSERT AS DECLARE @Product_ID int, @Product_inventory smallint, @Detial_number int DECLARE curs_sb_detail CURSOR FOR SELECT Product_ID, Product_inventory FROM inserted OPEN curs_sb_detail FETCH NEXT FROM curs_sb_detail INTO @Product_ID, @Product_inventory WHILE @@FETCH_STATUS = 0 BEGIN IF EXISTS (SELECT TOP 1 * FROM Product WHERE Product_ID = @Product_ID AND Product_inventory = @Product_inventory) BEGIN UPDATE Product SET Product_inventory = Product_inventory - @Detial_number WHERE Product_ID = @Product_ID AND Product_inventory = Detial_number IF @@ROWCOUNT = 0 OR @@ERROR <> 0 BEGIN CLOSE curs_sb_detail DEALLOCATE curs_sb_detail RAISERROR('库存修改不成功', 16, 1) RETURN END END FETCH NEXT FROM curs_sb_detail INTO @Product_ID, @Product_inventory END CLOSE curs_sb_detail DEALLOCATE curs_sb_detail修改成功
时间: 2023-06-23 21:10:33 浏览: 97
这是一个 SQL Server 数据库的触发器,用于在向 Product 表中插入数据时自动更新库存量。它会遍历插入的数据,对每个商品进行判断,如果该商品的库存量与插入数据中的库存量相同,则更新该商品的库存量为原库存量减去插入数据中的数量。如果更新不成功,则会抛出一个错误信息。这个触发器看起来是可以成功更新库存量的。
相关问题
create trigger ti_Buying on Buying for insert as declare @PID int,@quantity int select @PID=PID from inseted update products set quantity=quantity_@Squanty where PID=@PID go create trigger ti_Buying on Buying for insert as declare @PID int,@quantity int select @PID=PID from inseted update products set quantity=quantity_@Squanty where PID=@PID go ;
There seems to be a repetition in your code. The trigger `ti_Buying` on the table `Buying` is being created twice. Could you please clarify what you are trying to achieve with these triggers?
阅读全文