IF (@crncy_code = 'JMD') BEGIN
SELECT @total = isnull(sum(balance),0)
FROM [PROD_EIMDW_BOJ_REPORTS].[BOJOWN].[LOANS_TAB] blt
WHERE currency = 'JMD'
AND chrge = 'N' -- not charged off
AND dpd <= @p_days_past_upper_range
AND dpd >= @p_days_past_lower_range
AND NOT EXISTS(SELECT 1 FROM [BOJOWN].[LOANS_EXCLUSION] WHERE acid = blt.acid)
AND NOT EXISTS (SELECT acid FROM [PROD_EIMDW_Archive].[ARCHOWN].[FINCL10_ALR] WHERE BANK_ID= @bank_id AND acct_label = 'MUNDO' AND ACTIVE_FLAG = 'Y' AND DELETE_FLAG='N' AND acid = blt.acid)
SELECT @other_sum = isnull(sum(balance),0)
FROM [PROD_EIMDW_BOJ_REPORTS].[BOJOWN].[LOANS_TAB] blt
WHERE currency = 'JMD'
AND chrge = 'N' -- not charged off
AND dpd <= @p_days_past_upper_range
AND dpd >= @p_days_past_lower_range
AND NOT EXISTS (SELECT acid FROM [PROD_EIMDW_Archive].[ARCHOWN].[FINCL10_ALR] WHERE BANK_ID= @bank_id and DELETE_FLAG='N' AND acct_label = 'MUNDO' AND ACTIVE_FLAG = 'Y' AND acid = blt.acid)
AND EXISTS (SELECT 1 FROM [PROD_EIMDW_BOJ_REPORTS].[BOJOWN].[LOANS_EXCLUSION] WHERE acid = blt.acid)
set @loanCount = @total + @other_sum
Когда я запускаю функцию, она не возвращает суммирование двух переменных, а только возвращает значение, сохраненное в первой переменной. Также обратите внимание, когда я запускаю оператор вне функции, все в порядке. Ниже приведена инструкция возврата:
RETURN isnull(@loanCount,0)