SQL 用户定义函数返回 0 而不是浮点数

Posted

技术标签:

【中文标题】SQL 用户定义函数返回 0 而不是浮点数【英文标题】:SQL user defined function returns 0 instead of float 【发布时间】:2015-10-28 21:38:00 【问题描述】:

我想使用下面的代码创建一个函数,当我运行查询时返回的值是

21902/2061=0.094101

用作函数的代码

BEGIN
DECLARE @nat_weight INT =0;
DECLARE @mattype1 INT;
DECLARE @mattype2 INT;
DECLARE @mattype3 INT;
DECLARE @CycleHopper_02 INT;
DECLARE @PRCT_VAL FLOAT;
--DECLARE @me38_cycle_data_ndx INT;
-- get material type, need only hoppers 1-3, hopper 4,5,6 material type will never = 2
SET @mattype1 = (SELECT typehopper_01 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002);
SET @mattype2 = (SELECT typehopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002);
SET @mattype3 = (SELECT typehopper_03 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
SET @CycleHopper_02 = (SELECT CycleHopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
-- if material type=2 then add to @nat_weight ,  
IF @mattype1 = 2
    set @nat_weight = (SELECT cyclehopper_01 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
IF @mattype2 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
IF @mattype3 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_03 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 )
-- device hoper weight by natural weight,  0 if dev/0 
SET @PRCT_VAL= (SELECT COALESCE(CAST(@CycleHopper_02 AS FLOAT)/CAST(@nat_weight AS FLOAT),0));
IF @PRCT_VAL=1 
SET @PRCT_VAL=100

当我创建函数时,返回值为 0,这可能是什么原因?

    CREATE FUNCTION dbo.hprpct_02(@me38_cycle_data_ndx INT)
RETURNS INT
AS 
BEGIN
DECLARE @nat_weight INT =0;
DECLARE @mattype1 INT;
DECLARE @mattype2 INT;
DECLARE @mattype3 INT;
DECLARE @CycleHopper_02 INT;
DECLARE @PRCT_VAL FLOAT;
--DECLARE @me38_cycle_data_ndx INT;
-- get material type, need only hoppers 1-3, hopper 4,5,6 material type will never = 2
SET @mattype1 = (SELECT typehopper_01 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002);
SET @mattype2 = (SELECT typehopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002);
SET @mattype3 = (SELECT typehopper_03 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
SET @CycleHopper_02 = (SELECT CycleHopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
-- if material type=2 then add to @nat_weight ,  
IF @mattype1 = 2
    set @nat_weight = (SELECT cyclehopper_01 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
IF @mattype2 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 );
IF @mattype3 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_03 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=8002 )
-- device hoper weight by natural weight,  0 if dev/0 
SET @PRCT_VAL= (SELECT COALESCE(CAST(@CycleHopper_02 AS FLOAT)/CAST(@nat_weight AS FLOAT),0));
IF @PRCT_VAL=1 
SET @PRCT_VAL=100
RETURN @PRCT_VAL
END 

【问题讨论】:

你正在返回 int 【参考方案1】:

RETURNS INT 语句导致结果被强制转换为整数 - 在这种情况下为 0。你想要一个浮点数,所以相应地替换它

【讨论】:

【参考方案2】:

如果你只是声明一个 int 它将是 NULL。所以这段代码:

set @nat_weight =@nat_weight+ ... 

如果@nat_weight 未在您的第一个条件中设置(IF @mattype1 = 2),则计算结果为 NULL。

那么最后你有:

SET @PRCT_VAL=(SELECT COALESCE(CAST(@CycleHopper_02 AS FLOAT)/CAST(@nat_weight AS FLOAT),0))

如果你执行了 COALESCE(..., -13) ,则返回值将是 -13。

因此,将您希望默认为零的所有内容初始化为零。

【讨论】:

以上是关于SQL 用户定义函数返回 0 而不是浮点数的主要内容,如果未能解决你的问题,请参考以下文章

整数和浮点数及运算函数

为啥 Kivy 使用浮点数而不是整数?

Django的带除法的注释计数返回整数而不是浮点数

mod 函数是不是在 OpenGL 3.3 核心中返回 highp 浮点数?

SQL Server - 浮点数与 varchar

为啥神经网络输出浮点数而不是整数?