I’ve some case about executing stored procedures in SQL Server.
Maybe it’s unusual practice, but I’ve stored a list of stored procedure names in a table. The table is about like this (let’s call it TableFoo):
| SPId | SPName |
-------------------
| 1 | spr_name1|
| 2 | spr_name2|
| 3 | spr_name3|
| 4 | spr_name4|
...
I want to call/execute a list of stored procedure that generated from the query result on TableFoo, the query is about like this:
SELECT SPName
FROM TableFoo
WHERE SPId IN (1, 2, 3)
I want to execute the stored procedures sequentially per row
I mean I want to do this 🙂
SELECT EXEC(SpName)
FROM TableFoo
WHERE SPId IN (1, 2, 3)
but that does not work
It’s any solution beside using a CURSOR ?
Thanks in advance.
build a single batch that invokes all the procedures, using string aggregation, then run this batch. There are many string aggreate concatenation methods, but one of the most effective is the XML blackbox method: