我为除法创建了一个简单的存储过程。当我运行它时,它会四舍五入数字,即使它们是小数。例如,5.0/10.0=1
。为什么要这样做?
这是脚本:
GO
CREATE PROCEDURE uspDivide2Numbers2
@intValue1 AS DECIMAL
,@intValue2 AS DECIMAL
AS
SET NOCOUNT ON --Report only errors
DECLARE @intResult AS DECIMAL = 0
--Do calculation
SELECT @intResult = @intValue1 / @intValue2
--Display results
SELECT @intResult AS intResult
GO
uspDivide2Numbers2 5.0, 10.0
谢谢
最佳答案
您还没有为 DECIMAL 数据类型定义比例。
声明具有指定比例的存储过程参数。
@intValue1 AS DECIMAL(10,2)
,@intValue2 AS DECIMAL(10,2)
和
DECLARE @intResult AS DECIMAL(10,2)
这意味着@intResult 可以有小数点后两位数(即 scale =2 )和最多 10 位数字。 如果您没有定义 scale ,则 sql 假定 scale =0,因此您不会得到小数点后的数字。
关于SQL 存储过程除法四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16641834/