rmariotti
asked on
Execute stored procedure with inputs from SELECT
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
FROM SPInputs
EXEC sp_TestSP @var1, @var2, @var3, var4
SELECT var1, var2, var3, var4
FROM SPInputs
>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.
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.
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
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
ASKER
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
SELECT *
FROM fn_ResultSet(spi.var1, spi.var2, spi.var3, spi.var4)
CROSS JOIN SPInputs spi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select @var1=var1, @var2=var2, @var3=var3, @var4=var4 from SPInputs
exec sp_TestSP @var1, @var2, @var3, @var4