【发布时间】:2011-02-02 15:17:32
【问题描述】:
我有一个使用 try catch 块执行更新的存储过程。列 (discountPercentage) 设置为十进制数据类型。如果用户将 discountPercentage 作为小数以外的任何值插入,例如如果他们尝试插入 45%,我希望存储过程能够捕获。我正在使用 ERROR_MESSAGE 函数,由于某种原因它没有捕获错误消息。我仍然收到指出“将数据类型 varchar 转换为十进制时出错”的错误消息。到目前为止,这是我的代码:
alter procedure procUpdateFoodTitleConditionDiscount
(
@foodTitleId int,
@conditionId int,
@discountPercentage decimal(4,2),
@errorMessage varchar(100) output
)
as
begin
set @errorMessage = 'Discount updated'
begin try
update Discount
set discountPercentage = @discountPercentage
where foodTitleId = @foodTitleId and conditionId = @conditionId
if (@@ROWCOUNT = 0)
begin
set @errorMessage = 'Update not successful.'
end
end try
begin catch
if (ERROR_MESSAGE () like '%converting%')
begin
set @errorMessage = 'Please Insert Discount Percentage as a Decimal (ex 0.45)'
end
end catch
end;
declare @errorMessageValue varchar (100)
execute procUpdateFoodTitleConditionDiscount
@foodTitleId = '104',
@conditionId = '4',
@discountPercentage = '45%',
@errorMessage = @errorMessageValue output
print @errorMessageValue
【问题讨论】:
标签: sql-server tsql