Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 Server 2012中执行过程时没有出现错误_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

为什么我在SQL Server 2012中执行过程时没有出现错误

为什么我在SQL Server 2012中执行过程时没有出现错误,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,以下是我的代码: IF OBJECT_ID('spUpdateProductDiscount') IS NOT NULL DROP PROC spUpdateProductDiscount GO CREATE PROC spUpdateProductDiscount (@ProductID INT, @DiscountPercent INT) AS BEGIN BEGIN TRY UPDATE Products SET D

以下是我的代码:

IF OBJECT_ID('spUpdateProductDiscount') IS NOT NULL
    DROP PROC spUpdateProductDiscount
    GO

CREATE PROC spUpdateProductDiscount
    (@ProductID INT,    @DiscountPercent INT)
AS
BEGIN
    BEGIN TRY 
        UPDATE Products
        SET DiscountPercent = @DiscountPercent
        WHERE ProductID = @ProductID
    END TRY
    BEGIN CATCH
        IF @DiscountPercent <= 0
            PRINT 'The value for this column DiscountPercent must be a positive number';
    END CATCH
END;

我不知道为什么我的
PRINT
语句没有生效。我将其设置为如果
@DiscountPercent
小于或等于0,因此我不理解为什么
PRINT
语句没有启动,但它正在将行更改为-15.00的
DiscountPercent

也许您需要添加一个约束

alter table products add constraint chk_discount_percent check (discountpercent > 0)

除了Gordon提出的添加约束之外,我们还可以检查参数

IF OBJECT_ID('spUpdateProductDiscount') IS NOT NULL
    DROP PROC spUpdateProductDiscount
    GO

CREATE PROC spUpdateProductDiscount
    (@ProductID INT,    @DiscountPercent INT)
AS
BEGIN
    IF @DiscountPercent <= 0 BEGIN
        PRINT 'The value for this column DiscountPercent must be a positive number';
    END ELSE BEGIN
        UPDATE Products
        SET DiscountPercent = @DiscountPercent
        WHERE ProductID = @ProductID
    END
END;
如果对象ID('spUpdateProductDiscount')不为空
DROP PROC SpUpdateProduct折扣
去
创建PROC spUpdateProductDiscount
(@ProductID INT、@DiscountPercent INT)
作为
开始

如果@DiscountPercent
我不知道为什么我的打印语句没有生效
-因为
更新
没有引发异常?为什么你希望
更新
引发异常?你是否有一个约束或触发器定义折扣百分比不能为负?谢谢我添加了一个约束,但我没有把这些陈述转过来
IF OBJECT_ID('spUpdateProductDiscount') IS NOT NULL
    DROP PROC spUpdateProductDiscount
    GO

CREATE PROC spUpdateProductDiscount
    (@ProductID INT,    @DiscountPercent INT)
AS
BEGIN
    IF @DiscountPercent <= 0 BEGIN
        PRINT 'The value for this column DiscountPercent must be a positive number';
    END ELSE BEGIN
        UPDATE Products
        SET DiscountPercent = @DiscountPercent
        WHERE ProductID = @ProductID
    END
END;