[英]Arithmetic overflow error converting varchar to data type numeric in stored procedure
I'm getting a Arithmetic overflow error converting varchar to data type numeric. 我收到将varchar转换为数值数据类型的算术溢出错误。 In my stored procedure although only when it has to make the calculations.
在我的存储过程中,虽然只有在必须进行计算时。 Here is my code, I'm using a convert still seems to be something wrong.
这是我的代码,我使用的转换似乎还是有问题。
set @Text = @Text + 'Something:' +
(select case when s.ATF = '' then '0'
else Convert(varchar,100 *((s.ATF *1.00) /
(s.Total * 1.00))) + '%'
end
from Maintenance.dbo.stats s
where s.ID = @ID) + CHAR(13) + CHAR(10)
Thanx! 感谢名单!
The problem is that you take a varchar
and multiply it by a numeric value. 问题是您将一个
varchar
乘以一个数值。 That forces an implicit conversion to a numeric type. 这将强制将隐式转换为数字类型。 You should specify conversion explicitly.
您应该明确指定转换。 I chose
decimal(38,6)
but you can change that. 我选择了
decimal(38,6)
但是您可以更改它。
set @Text = @Text + 'Something:' +
(select case when s.ATF = '' then '0'
else Convert(varchar, 100 * cast(s.ATF as decimal(38,6)) /
cast(s.Total as decimal(38,6))) + '%'
end
from Maintenance.dbo.stats s
where s.ID = @ID) + CHAR(13) + CHAR(10)
You can see how this wrong conversion works on that simple example: 您可以在该简单示例上看到这种错误转换的工作方式:
declare @x as varchar(50) = '11'
select @x * 1.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.