views:

25

answers:

2

I have an input db2 table with two elements: loan_number, debt_to_income; this table's name is #Input_Table. I am trying to run a test the function by running a SQL program against this table. The problem is that the function's element is not being recognized in the SQL program for some reason, maybe I have been looking at to long? I need to validate that the output in the table will output in a order by the debt_to_income field.

Here is the function code:

 ALTER FUNCTION [dbo].[FN_DTI_BANDS]
(
    -- the parameters for the function here
    @FN_DTI_Band            decimal(4,3)
     )
RETURNS varchar(16)
AS
BEGIN

declare @Return varchar(16)

select @Return =

        Case
        when @FN_DTI_Band is NULL then ' Missing'
        WHEN @FN_DTI_Band = 00.00 then ' Missing'
        When @FN_DTI_Band < = 0.31 then 'Invalid'
        When @FN_DTI_Band between 0.31 and 0.34 then '31-34'
        When @FN_DTI_Band between 0.34 and 0.38 then '34-38'
        When @FN_DTI_Band >= 0.38 then '38+'
        else null end
            -- Return the result of the function
            RETURN @Return
END

Here is the T-SQL test program:

SELECT  loan_number,dbo.FN_DTI_BANDS(debt_to_income)as FN_DTI_Band

from #Input_table

SELECT      COUNT(*), FN_DTI_Band
FROM  #Input_table
GROUP BY FN_DTI_Band
ORDER BY FN_DTI_Band

Here is the error:

Msg 207, Level 16, State 1, Line 7
Invalid column name 'FN_DTI_Band'.
Msg 207, Level 16, State 1, Line 5
Invalid column name 'FN_DTI_Band'.

Can someone help me spot what I am overlooking? Thank you!

A: 

Try prepending "dbo" onto the name of the function.

Select Count(*), dbo.FN_DTI_Band
From....
Randy Minder
A: 

the table #input_table does not have a column called FN_DTI_Band.

Just the result of the first select statement has that column name.

You need to make the first select statement a sub query of the 2nd

Something like this:

SELECT COUNT(*), T.FN_DTI_Band
FROM 
(
   SELECT  loan_number,dbo.FN_DTI_BANDS(debt_to_income) as FN_DTI_Band
   from #Input_table
) T
GROUP BY T.FN_DTI_Band
ORDER BY T.FN_DTI_Band
Hogan
tried it and go a new error: Msg 8115, Level 16, State 8, Line 2Arithmetic overflow error converting numeric to data type numeric. So I tried: use TESTSELECT COUNT(*), t.FN_DTI_BAND from (SELECT loan_number, case when debt_to_income = 0 then 0 else dbo.FN_DTI_BANDS(debt_to_income) END as FN_DTI_Band from #T2 )T GROUP BY t.FN_DTI_Band ORDER BY t.FN_DTI_Bandstill not quite right...
JMS49
some of the data input rows have 0.0000 in the debt to income column
JMS49
you can use a where in the select statement to take those out.
Hogan
Thanks Hogan, the boss still wants them in.. still trying to get this to work...
JMS49
why do the conversion at all... just declare the parameter as numeric not decimal(4,3)
Hogan
I just did not think of it, SQL is still somewhat new to me.. Thank you for pointing this out, most appreciated!
JMS49