简体   繁体   English

SQL Server函数始终返回NULL

[英]SQL Server function always returns NULL

I'm pulling my hair out here. 我把头发拉到这里。 The function below always returns null, even though it works fine if I pull the code out into a query window and set the input parameters manually. 下面的函数总是返回null,即使它将代码拉出到查询窗口并手动设置输入参数也能正常工作。 The idea is that I have a different query based on the month, because each month's average value is stored in a different column. 这个想法是我根据月份有不同的查询,因为每个月的平均值存储在不同的列中。 I know, the table's not well normalized, but it's what I have to work with. 我知道,桌子没有很好地规范化,但这是我必须要做的事情。 What am I missing? 我错过了什么?

ALTER FUNCTION [dbo].[fn_currentShareBal]
(
@currentDate    datetime,
@account        integer,
@acctType       varchar
)  
RETURNS money AS  
BEGIN

DECLARE @dte char(10)
DECLARE @returnVal money
DECLARE @month int

SET @dte = CONVERT(char(10), @currentDate, 101)
SET @month = MONTH(@currentDate)

-- because of the table strucure the actual query depends on the month
IF @month = 1 SET @returnVal = (SELECT SUM(avg1)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 2 SET @returnVal = (SELECT SUM(avg2)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 3 SET @returnVal = (SELECT SUM(avg3)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 4 SET @returnVal = (SELECT SUM(avg4)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 5 SET @returnVal = (SELECT SUM(avg5)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 6 SET @returnVal = (SELECT SUM(avg6)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 7 SET @returnVal = (SELECT SUM(avg7)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 8 SET @returnVal = (SELECT SUM(avg8)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 9 SET @returnVal = (SELECT SUM(avg9)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 10 SET @returnVal = (SELECT SUM(avg10)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 11 SET @returnVal = (SELECT SUM(avg11)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

IF @month = 12 SET @returnVal = (SELECT SUM(avg12)
                        FROM COSHAVG
                        WHERE cuID = @account
                        AND avgBalanceMonth =  @dte
                        AND acctType = @acctType)

RETURN @returnVal

END
GO

I think the problem is with your function definition. 我认为问题在于你的函数定义。 It's hard to tell as you haven't posted your table's definition. 很难说你没有发表你的表的定义。

Does this work? 这有用吗? If not, can you post a scripted Create on the COSHAVG table? 如果没有,您可以在COSHAVG表上发布脚本化的创建吗?

ALTER FUNCTION [dbo].[fn_currentShareBal]
(
    @currentDate    datetime,
    @account        integer,
    @acctType       varchar(50)
)  
RETURNS money
AS  
BEGIN
    DECLARE @dte varchar(20)
    DECLARE @returnVal money
    DECLARE @month int

    SET @dte = CONVERT(varchar, @currentDate, 101)
    SET @month = MONTH(@currentDate)

    SELECT
        @returnVal=SUM(CASE
            WHEN @month = 1 THEN avg1
            WHEN @month = 2 THEN avg2
            WHEN @month = 3 THEN avg3
            WHEN @month = 4 THEN avg4
            WHEN @month = 5 THEN avg5
            WHEN @month = 6 THEN avg6
            WHEN @month = 7 THEN avg7
            WHEN @month = 8 THEN avg8
            WHEN @month = 9 THEN avg9
            WHEN @month = 10 THEN avg10
            WHEN @month = 11 THEN avg11
            WHEN @month = 12 THEN avg12
        END)
    FROM COSHAVG
    WHERE cuID = @account
    AND avgBalanceMonth =  @dte
    AND acctType = @acctType

    RETURN @returnVal
END
GO

Others have pointed out things to look for in the comments, but here's a cleaner way to rewrite the function (no real change in behavior, just easier to maintain): 其他人已经在评论中指出要查找的内容,但是这里有一种更简洁的方式来重写函数(没有真正的行为改变,只是更容易维护):

ALTER FUNCTION [dbo].[fn_currentShareBal]
    (
      @currentDate DATETIME
    , @account INTEGER
    , @acctType VARCHAR (100) 
    )
RETURNS MONEY
AS 
    BEGIN
        DECLARE @dte CHAR(10)
        DECLARE @returnVal MONEY
        DECLARE @month INT
        SET @dte = CONVERT(CHAR(10), @currentDate, 101)
        SET @month = MONTH(@currentDate) 
         -- because of the table strucure the actual query depends on the month 

        SELECT  @returnVal = SUM(CASE WHEN @month = 1 THEN avg1
                                      WHEN @month = 2 THEN avg2
                                    /*...*/
                                      WHEN @month = 12 THEN avg12
                                 END)
        FROM    COSHAVG
        WHERE   cuID = @account
                AND avgBalanceMonth = @dte
                AND acctType = @acctType


        RETURN @returnVal
    END 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM