为什么我在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;