2

I want to define a scaler function which in that I'm going to return the result into a variable but I do not know how to do this.

CREATE FUNCTION dbo.Funname ( @param int )
RETURNS INT
AS
declare @returnvar int
select  @returnvar = select colname from tablename where someconditions = something
return(@returnvar)

I want to make a function something like the top. I mean the result of the select statement which is:

select colname from tablename where someconditions = something

Is only a single cell and we are sure about it. I want to store it into a variable and return it from the function. How can I implement this thing?

1 Answer 1

3

I should probably mention that scalar UDFs do come with a considerable health warning and can cause performance issues depending upon how you use them.

Here's an example though.

CREATE FUNCTION dbo.Funname ( @param INT )
RETURNS INT
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN (SELECT number FROM master.dbo.spt_values WHERE number < @param)
END

In the above example I didn't use a variable as it is redundant. The version with variable is

BEGIN
DECLARE @Result int
SET @Result = (SELECT number FROM master.dbo.spt_values WHERE number < @param)
RETURN @Result
END

For both of the above you would need to be sure the Query returned at most one row to avoid an error at runtime. For example

select dbo.Funname(-1) Returns -32768

select dbo.Funname(0) Returns error "Subquery returned more than 1 value."

An alternative syntax would be

BEGIN
DECLARE @Result int
SELECT @Result = number FROM master.dbo.spt_values WHERE number < @param
RETURN @Result
END

This would no longer raise the error if the subquery returned more than one value but you would just end up with an arbitrary result with no warning - which is worse.

Following Comments I think this is what you need

CREATE FUNCTION dbo.getcustgrade(@custid CHAR(200)) 
RETURNS INT 
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
        RETURN
        ( SELECT [cust grade]
        FROM    ( SELECT  customerid,
                         DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) AS [cust grade]
                FROM     Orders
                GROUP BY CustomerID
                )
                d
        WHERE   customerid = @custid
        )
END
Sign up to request clarification or add additional context in comments.

3 Comments

i wrote this query in conclusion but it returns error in creating the funtion: CREATE FUNCTION dbo.getcustgrade ( @custid char(200) ) RETURNS INT WITH RETURNS NULL ON NULL INPUT AS begin return(select Orders.CustomerID, DENSE_RANK() OVER (ORDER BY count(*) desc) AS [cust grade] from Orders where customerid = @custid group by CustomerID ) end
the error is: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
You can't return both Orders.CustomerID and the rank. You would just need to select the Rank. That query doesn't look right anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.