SQL 用户定义函数 returns 0 而不是 float

SQL user defined function returns 0 instead of float

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

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 

RETURNS INT 语句导致结果被转换为整数 - 在本例中为 0。你想要一个浮动,所以相应地替换它

如果你只是声明一个 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),return 值将为 -13。

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