CREATE TABLE [dbo].[OrderDetails](
[OrderID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[UnitPrice] [int] NOT NULL,
[Quantity] [tinyint] NULL,
[Discount] [int] NULL,
[IsActive] [bit] NULL,
[IsDeleted] [bit] NULL
) ON [PRIMARY]
我想创建一个功能,检查插入的数量是否大于库存数量,然后阻止用户执行。
答案 0 :(得分:0)
GO
SET QUOTED_IDENTIFIER ON; GO
在dbo.OrderDetails上创建TRIGGER dbo.PreventQtyInsert 插入后 如 开始 DECLARE @returnQuantity INT, @Quantity INT, @ProductID INT, @GetQuantityFromProductsTable INT;
SELECT @returnQuantity = dbo.ReturnUnitsInStock ( @Quantity , @ProductID ) ;
IF @returnQuantity = 0
BEGIN
RAISERROR ( 'This vendor''s credit rating is too low to accept new purchase orders.' , 16 , 1 ) ;
ROLLBACK TRANSACTION;
RETURN;
END;
ELSE
BEGIN
SET @GetQuantityFromProductsTable = ( SELECT UnitsInStock FROM Products WHERE ProductID = @ProductID ) ;
UPDATE OrderDetails SET Quantity = @GetQuantityFromProductsTable - @Quantity
WHERE ProductID = @ProductID;
END;
END;
GO