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

时间:2015-10-28 21:38:01

标签: sql-server-2008 user-defined-functions

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

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 

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

   WITH BaseData AS
(
     select 
        vendor_code,
        RTRIM(LTRIM(cast(datename(month, [CLOSED_DATE]) as char(15))))+',' + RTRIM(LTRIM(cast(year([CLOSED_DATE]) as char(20)))) as [CLOSED_DATE],
        count(vendor_code) as [No. of Case] 
   from #teamp t WITH (NOLOCK)
      where 
       [CLOSED_DATE] is not null
  group by 
       vendor_code, CLOSED_DATE
)   
SELECT ISNULL([November,2012],0)AS[November,2012],ISNULL([December,2012],0)AS[December,2012],ISNULL([January,2013],0)AS[January,2013],ISNULL([February,2013],0)AS[February,2013],ISNULL([March,2013],0)AS[March,2013],ISNULL([April,2013],0)AS[April,2013],ISNULL([May,2013],0)AS[May,2013],ISNULL([June,2013],0)AS[June,2013],ISNULL([July,2013],0)AS[July,2013],ISNULL([August,2013],0)AS[August,2013],ISNULL([September,2013],0)AS[September,2013],ISNULL([October,2013],0)AS[October,2013],ISNULL([November,2013],0)AS[November,2013],ISNULL([December,2013],0)AS[December,2013],ISNULL([January,2014],0)AS[January,2014],ISNULL([February,2014],0)AS[February,2014],ISNULL([March,2014],0)AS[March,2014],ISNULL([April,2014],0)AS[April,2014],ISNULL([May,2014],0)AS[May,2014],ISNULL([June,2014],0)AS[June,2014],ISNULL([July,2014],0)AS[July,2014],ISNULL([August,2014],0)AS[August,2014],ISNULL([September,2014],0)AS[September,2014],ISNULL([October,2014],0)AS[October,2014],ISNULL([November,2014],0)AS[November,2014],ISNULL([December,2014],0)AS[December,2014],ISNULL([January,2015],0)AS[January,2015],ISNULL([February,2015],0)AS[February,2015],ISNULL([March,2015],0)AS[March,2015],ISNULL([April,2015],0)AS[April,2015],ISNULL([May,2015],0)AS[May,2015],ISNULL([June,2015],0)AS[June,2015],ISNULL([July,2015],0)AS[July,2015],ISNULL([August,2015],0)AS[August,2015],ISNULL([September,2015],0)AS[September,2015],ISNULL([October,2015],0)AS[October,2015]FROM BaseData
PIVOT
(
    sum([No. of Case])
    FOR CLOSED_DATE IN ([November,2012],[December,2012],[January,2013],[February,2013],[March,2013],[April,2013],[May,2013],[June,2013],[July,2013],[August,2013],[September,2013],[October,2013],[November,2013],[December,2013],[January,2014],[February,2014],[March,2014],[April,2014],[May,2014],[June,2014],[July,2014],[August,2014],[September,2014],[October,2014],[November,2014],[December,2014],[January,2015],[February,2015],[March,2015],[April,2015],[May,2015],[June,2015],[July,2015],[August,2015],[September,2015],[October,2015])
) AS PivotTable
如果在第一个条件中未设置@nat_weight,则

将评估为NULL(IF @ mattype1 = 2)。

然后最后你有:

set @nat_weight =@nat_weight+ ... 

如果您进行了COALESCE(..., - 13)投注,则返回值为-13。

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