all groups > sql server programming > september 2003 >
You're in the

sql server programming

group:

Dynamic execution of sprocs in Tsql?


Dynamic execution of sprocs in Tsql? Greg
9/11/2003 9:52:05 PM
sql server programming: I would like to execute a list of sprocs based on the
values of a table column (the values are the sproc names).
How would I go about doing that? I want this to execute
on a defined schedule.

I have table 'statdefs':
cols: id, sprocname, sprocargs

So here's the flow I am going for:

-> get sproc list - select sprocname, sprocargs from statdefs

-> iterate through that recordset
-> execute sprocname sprocargs <- sprocargs is optional
-> loop

I have done this many times on an app server or other
client, but never on the sql server itself. What I am
doing is a statistics collection on our main database. So
I thought it would be nice to run a job nightly with a
dynamic list of sprocs, but I don't know how at this point.

Any help is appreciated.

Greg

Dynamic execution of sprocs in Tsql? Raghavendra Narayana
9/11/2003 10:05:24 PM
Get the recordset into a cursor.
Loop through the cursor
Construct string to run Stored Procedure
Run the constructed string with SP_ExecuteSQL system SP.

sp_executesql: Executes a Transact-SQL statement or batch
that can be reused many times, or that has been built
dynamically. The Transact-SQL statement or batch can
contain embedded parameters.

Cheers,
Raghavendra Narayana

[quoted text, click to view]
AddThis Social Bookmark Button