Execute stored procedure with inputs from SELECT

Posted on 2007-07-25
Last Modified: 2012-05-05
Is there a way to execute a stored procedure using the results from a select?   I have a stored procedure who's signature takes in four variables I want to be able to call the stored procedure by writing a select to retrieve the four variables, which may return multiple rows.  For example

EXEC sp_TestSP @var1, @var2, @var3, var4
SELECT var1, var2, var3, var4
Question by:rmariotti
    LVL 15

    Expert Comment

    declare @var1 int, @var2 int, @var3 int, @var4 int

    select @var1=var1, @var2=var2, @var3=var3, @var4=var4 from SPInputs

    exec sp_TestSP @var1, @var2, @var3, @var4
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >Is there a way to execute a stored procedure using the results from a select?
    not like that.
    how many rows do you get?

    so either, you get the procedure into a function, or, if your query returns only 1 row, or you need to do a cursor on the SELECT.

    LVL 68

    Expert Comment

    No.  You could modify the sp to process data from a temp table, load the temp table prior to calling the sp, then passing a flag to the stored proc to let it know to process the value in the temp table rather than in the params.  For example:

    CREATE TABLE #params (
        var1 VARCHAR(30),
        var2 INT,
        var3 INT,
        var4 DATETIME
    INSERT INTO #params
    SELECT var1, var2, var3, var4
    FROM SPInputs

    EXEC sp_TestSP 'UseTemp', -1

    Author Comment

    If I made it a table-valued UDF, how would I join to it?   Basically, the select can return multiple rows, and each of those rows contains variables that should be passed into the function.  In addition, the function will return multiple rows.  So if the function returns 3 rows per call and the SPInputs table has 2 rows, the SELECT would return a ResultSet of 6 rows...

          SELECT *
          FROM fn_ResultSet(spi.var1, spi.var2, spi.var3, spi.var4)
          CROSS JOIN SPInputs spi
    LVL 142

    Accepted Solution

    in sql server 2000, you cannot do that in a single sql.
    in sql server 2005, you could do that indeed using the CROSS APPLY:

          SELECT *
          FROM SPInputs spi
          CROSS APPLY dbo.fn_ResultSet(spi.var1, spi.var2, spi.var3, spi.var4) r


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    732 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

    22 Experts available now in Live!

    Get 1:1 Help Now