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=
42703Msg 7350, Level 16, State 2, Line 5
Cannot get the column information from OLE DB provider “MSDASQL” for linked server “SrvLinnk”
Well, the function should read like this at least if it’s for SQL Server: what you have above is wrong
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
The question as it stands makes no sense I’m sorry to say…