我在這裏拉我的頭髮。下面的函數總是返回null,即使它能正常工作,如果我將代碼拖出到查詢窗口並手動設置輸入參數。這個想法是,我有一個基於月份的不同查詢,因爲每個月的平均值都存儲在不同的列中。我知道,桌子的標準化程度不是很好,但這是我必須努力的。我錯過了什麼?SQL Server函數總是返回NULL
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
是'COSHAVG ''dbo'計劃的一部分? – Glenn
什麼是avgBalanceMonth?將它與10個字符的日期故意相比較? – hatchet
你是怎麼調用這個函數的?你可能會考慮爲你的輸入參數設置默認值,看看是否改變了行爲......如果你的話可能你的調用代碼是行爲不端...例如:在.NET中,內置解決方案可能會將NULL字符串轉換爲空白...只是一個想法 – Matthew