You are on page 1of 4

Can I execute a stored proc multiple times (with different parameter values each

time) without a cursor or while loop


sqlservercentral.com/Forums/Topic952819-392-1.aspx

Can I execute a stored proc multiple times (with different parameter values each time) without a cursor or while loop

Author
Message

Posted 7/15/2010 3:45:18 AM

At the moment, my code looks a bit like this:


exec dbo.usp_my_stored_proc 1, 12, 10000;
exec dbo.usp_my_stored_proc 2, 12, 10000;
exec dbo.usp_my_stored_proc 3, 13, 9000;
Ten Centuries
exec dbo.usp_my_stored_proc 7, 14, 10000;
exec dbo.usp_my_stored_proc 7, 15, 11000;
exec dbo.usp_my_stored_proc 8, 16, 10000;
.
Group: General .
Forum Members .
Last Login: and so on.
11/13/2016
1:57:19 AM There must be a way I can move those parameter values into a table (of three cols in this case) and
Points: 1,006, then execute the stored proc N times based on the results of a select statement.
Visits: 1,898
I could do it easily enough with a while loop or a cursor, but is there a way I can do it without these?

Cheers

GPO
1/4
GPO

One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly
important.
Bertrand Russell

Posted 7/15/2010 7:28:10 AM

Hi There,

(edited my post)

--// SAMPLE PROCEDURE


CREATE PROCEDURE dbo.usp_my_stored_proc
@value1 INT,
@value2 INT,
@value3 INT
AS
BEGIN
SELECT @value1 AS 'Value1'
, @value2 AS 'Value2'
, @value3 AS 'Value3'
END
RETURN
SSC- GO
Enthusiastic
--// SAMPLE DATA
DECLARE @tbl TABLE
(
ID INT IDENTITY,
Group: General
Forum Members value1 NVARCHAR(MAX),
Last Login: value2 NVARCHAR(MAX),
9/18/2015 value3 NVARCHAR(MAX)
8:34:45 AM )
Points: 181,
Visits: 466 INSERT INTO @tbl
SELECT 1, 12, 10000
UNION SELECT 2, 12, 10000
UNION SELECT 3, 13, 9000
UNION SELECT 7, 14, 10000
UNION SELECT 7, 15, 11000
UNION SELECT 8, 16, 10000

--// WHAT YOU NEED


DECLARE @i INT, @max INT, @query NVARCHAR(MAX)

SET @query=''

SELECT @query= @query +'EXEC dbo.usp_my_stored_proc


'
+ value1 + ', '
+ value2 + ', '
+ value3 + ';
'
FROM @tbl

EXEC (@query)

2/4
--// DROP OF SAMPLE PROCEDURE
DROP PROCEDURE dbo.usp_my_stored_proc

I just recreated your script and ran it in one go.

_____________________________________________
Quatrei Quorizawa
:):D:P;)
MABUHAY PHILIPPINES!

"Press any key...


Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson

Edited: 7/15/2010 7:54:31 AM by Quatrei.X

Posted 7/15/2010 7:55:01 AM

SSC Rookie GPO (7/14/2010)

At the moment, my code looks a bit like this:


exec dbo.usp_my_stored_proc 1, 12, 10000;
exec dbo.usp_my_stored_proc 2, 12, 10000;
Group: General exec dbo.usp_my_stored_proc 3, 13, 9000;
Forum Members exec dbo.usp_my_stored_proc 7, 14, 10000;
Last Login: exec dbo.usp_my_stored_proc 7, 15, 11000;
11/1/2010 exec dbo.usp_my_stored_proc 8, 16, 10000;
11:19:46 AM .
Points: 39, Visits:
150 can use table valued parameters allows us to pass a table as a single parameter to a stored
procedure.

Create Type YouTable as Table


(
col1 int,
col2 int,
col3 int
)

declare @T YouTable

insert into @T values


(1, 12, 10000),
(2, 12, 10000),
(3, 13, 9000)

exec dbo.usp_my_stored_proc @T

but first you must change your sp to receive a table valued parameter.

Edited: 7/15/2010 8:19:35 AM by changbluesky

3/4
4/4

You might also like