[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Execute stored procedure with inputs from SELECT

Posted on 2007-07-25
Medium Priority
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

ID: 19569043
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19569066
>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 70

Expert Comment

by:Scott Pletcher
ID: 19569379
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
SELECT var1, var2, var3, var4

EXEC sp_TestSP 'UseTemp', -1

Author Comment

ID: 19569817
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 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 19571863
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

873 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