Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 创建一个函数来检查插入的数量是否大于库存数量_Sql_Function_Stored Procedures_Triggers - Fatal编程技术网

Sql 创建一个函数来检查插入的数量是否大于库存数量

Sql 创建一个函数来检查插入的数量是否大于库存数量,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

我想创建一个函数,该函数将检查插入的数量是否大于库存数量,然后阻止用户执行。

GO

在上设置带引号的\u标识符; 去

在dbo.OrderDetails上创建触发器dbo.PreventQtyInsert 插入后 作为 开始 声明@returnQuantity INT, @数量INT, @ProductID INT, @GetQuantityFromProductsTable INT

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;