views:

21

answers:

1

I am still really new to SQL functions. I am trying to figure out how to use the in a SQL program properly. I am wanting to test scalar UDF's that I have created to see that the return the data correctly and can return a large quantity of data in order. I am not sure what is wrong with my syntax in the SQL to use the function as this is my first attempt. Can someone steer me in the right direction?

Here is an example.

Function code :

SET ANSI_NULLS_ON
GO
GET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION dbo.FN_LTV_Ranges

(

   @LTV_RANGE     decimal(4,3)
)

Returns variable (160
as 
Begin

declare @Return     varchar(16)
select  @Return =

        Case 
        When @LTV_Range is NULL then 'Missing'
        When @LTV_Range is 00.00 then 'Missing'
        When @LTV_Range <= 0.75 then '<=0.75'
        When @LTV_Range between 0.75 and 0.80 then '75-80'
        When @LTV_Range between 0.80 and 0.90 then '80-90'
        When @LTV_Range between 0.90 and 1.00 then '90-100'
        When @LTV_Range >= 100 then '100+'
        else null end

        Return &Return
END

here is SQL program to call and test above function:

declare @LTV_Range         decimal(4,3)

Select top 600   s.LNumber

from OPENQUERY (SvrLink,  '

Select Lnumber, dbo.FN_LTV_Range(@LTV_Range)

 from some_table s
        where s.LNumber > '0'
          group by @LTV_Range
          Order by @LTV_Range

for Fetch only with UR')

Here is error returned on attempt to run SQL program:

OLE CB provider "MSDASQL" for linked server "SvrLink" returned message "(IBM)(CLI Driver) (DB2/LINUXX8641) SQL0306N "@LTV_RANGE" is not valid in context where it is used. SQLSTATE= 42703

Msg 7350, Level 16, State 2, Line 5 Cannot get the column information from OLE DB provider "MSDASQL" for linked server "SrvLinnk"

A: 

Well, the function should read like this at least if it's for SQL Server: what you have above is wrong

ALTER FUNCTION dbo.FN_LTV_Ranges
(

   @LTV_RANGE     decimal(4,3)
)
Returns varchar(16)
as 
Begin
declare @Return     varchar(16)
select  @Return =
        Case 
        When @LTV_Range is NULL then 'Missing'
        When @LTV_Range = 0 then 'Missing'
        When @LTV_Range <= 0.75 then '<=0.75'
        When @LTV_Range between 0.75 and 0.80 then '75-80'
        When @LTV_Range between 0.80 and 0.90 then '80-90'
        When @LTV_Range between 0.90 and 1.00 then '90-100'
        When @LTV_Range >= 100 then '100+'
        else null end
        Return @Return
END

For decimal(4,3) your min/max is +/- 9.999 so why this "When @LTV_Range >= 100 then '100+'"?

Next, why have OPENQUERY submitting a SQL call to a DB2 instance that includes a SQL Server function?

I assume you want the function call + grouping + ordering outside. And where do you set @LTV_Range?

Finally, grouping + ordering on @LTV_Range is pointless: it's a single value so I assume you mean to group/order on the result of the function call

declare @LTV_Range decimal(4,3)
Select top 600
   s.LNumber, dbo.FN_LTV_Range(@LTV_Range)
from
     OPENQUERY (SvrLink,  '
Select Lnumber
 from some_table s
        where s.LNumber > '0'
for Fetch only with UR')
group by dbo.FN_LTV_Range(@LTV_Range)
Order by dbo.FN_LTV_Range(@LTV_Range)

The question as it stands makes no sense I'm sorry to say...

gbn
I am sorry I mistyped the function information regarding the returns variable the closing parentheses was inadvertently typed as a zero. The values returned are percents as represented in whole numbersThe reason for the need to group and order the returned data is that when the developer is using the function in summary type (using say Count(*) reports then the all of the potential returned values are in order, on the lines of this type of output:Lnumber LTV_Range 123 Missing 300 <=75 250 75-90 75 80-90 2 100+
JMS49