当我在SQL Server 2012中执行我的过程时,为什么我的错误没有开始

Jos*_*ght -2 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 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;
Run Code Online (Sandbox Code Playgroud)
EXEC spUpdateProductDiscount 1999, -15.00
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我的PRINT陈述没有踢.我把它设置为if @DiscountPercent小于或等于0所以我不明白为什么PRINT声明没有踢但是它正在改变行到DiscountPercent- 15.00.

Gor*_*off 5

也许你需要添加一个约束?

alter table products add constraint chk_discount_percent check (discountpercent > 0)
Run Code Online (Sandbox Code Playgroud)