Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1067255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:08:04+00:00 2026-05-16T20:08:04+00:00

I want to define a scaler function which in that I’m going to return

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:08:04+00:00Added an answer on May 16, 2026 at 8:08 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.