SQL - Stored Procedure or Function inside a SELECT

Posted on 2007-08-01
Last Modified: 2008-01-09

Is it possable to run a SQL Stored Procedure or a Function inside a SELECT statement?

If so, can you please give me an example? :)

Thank you!
Question by:Endelm
    LVL 75

    Accepted Solution

    u can call functions inside a select statement

    create function dbo.RetSum(@i int, @j int)
    returns int
    return (@i+@j)

    SELECT dbo.RetSum(1,2)
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >Is it possable to run a SQL Stored Procedure inside a SELECT statement?

    >Is it possable to run a SQL Stored Function inside a SELECT statement?
    yes, see comment of aneeshattingal
    LVL 68

    Expert Comment

    You can use a scalar function in place of any single value in a SELECT, so it can appear in the SELECT list, WHERE clause, etc..

    You can use table-based functions in place of a table within a SELECT ** but the parameters to the table-based functions must be fully known at the start of the query, that is, you cannot use a result column of a query in a table-based function w/i a query**.  For example, this is OK:
    SET @date = GETDATE() - 30
    SELECT ...
    FROM dbo.aTableBasedFunction (@date)
    since @date is known when the SELECT is started.  This is NOT allowed:
    SELECT ...
    FROM table1
    CROSS JOIN dbo.aTableBasedFunction (table1.column1)
    since "table1.column1" varies during the query, it cannot be resolved *prior* to running the SELECT, as required.
    LVL 68

    Expert Comment

    For scalar functions, otoh, you can use table columns.  For example, this:

    SELECT dbo.aScalarFunction(table1.column1) AS [Single-Value Function Result]
    FROM table1
    WHERE dbo.anotherScalarFunction(table1.column2) = 'ABC'

    is OK.

    Author Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now