Sql 创建一个函数来检查插入的数量是否大于库存数量
我想创建一个函数,该函数将检查插入的数量是否大于库存数量,然后阻止用户执行。GO 在上设置带引号的\u标识符; 去 在dbo.OrderDetails上创建触发器dbo.PreventQtyInsert 插入后 作为 开始 声明@returnQuantity INT, @数量INT, @ProductID INT, @GetQuantityFromProductsTable INTSql 创建一个函数来检查插入的数量是否大于库存数量,sql,function,stored-procedures,triggers,Sql,Function,Stored Procedures,Triggers,我想创建一个函数,该函数将检查插入的数量是否大于库存数量,然后阻止用户执行。GO 在上设置带引号的\u标识符; 去 在dbo.OrderDetails上创建触发器dbo.PreventQtyInsert 插入后 作为 开始 声明@returnQuantity INT, @数量INT, @ProductID INT, @GetQuantityFromProductsTable INT CREATE TABLE [dbo].[OrderDetails]( [OrderID] [int] NO
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]
去想想哪个
选择
会给你正确的回答。。。然后将其添加到您的应用程序中。
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;