Solved

Execute a list of stored procedures from a query result

Posted on 2014-02-07
3
237 Views
Last Modified: 2014-02-07
I'm wondering if it possible to execute (possibly through dynamic sql) a number of stored procedures returned from a query.  

For example if a query was run to retrieve the name of stored procedures from sysobjects that meet a criteria and the names minus the prefix sp_ was returned could these be used to geenrate a exec SQL statement whereby the stored procedures returned could then be run.

So the query result set from:

select SUBSTRING(name,4,20) from sysobjects where id in ( select ID from syscomments where ltrim(Rtrim([text])) like '%tblSales%')



Might return the 2 stored procedures
SALESWEEKLY
SALESMONTHLY

which are actually sp_SALESWEEKLY AND sp_SALESMONTHLY

Could code then be produced to run the initial query and then execute all the procedures returned from it.

Something like

DECLARE @SQL VARCHAR(max)

Set @SQL = 'EXEC SP_' + (first Stored Procedure name)

and then loop until all are executed.

I think this would be very useful if it could be achieved.


Many thanks
0
Comment
Question by:DanP1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39842052
Yes it can be done, by using a cursor as below

DECLARE @SQL NVARCHAR(2000)
DECLARE @SPName NVARCHAR(50)
DECLARE T CURSOR 
FOR 
select SUBSTRING(name,4,20) from sysobjects where id in ( select ID from syscomments where ltrim(Rtrim([text])) like '%tblSales%') 
OPEN T 
FETCH T INTO @SPName
WHILE @@FETCH_STATUS = 0 
BEGIN
 SET @SQL = ' 'EXEC SP_' +@SPName
 EXEC (@SQL)
 FETCH T INTO @SPName
END
CLOSE T
DEALLOCATE T

Open in new window

0
 

Author Closing Comment

by:DanP1980
ID: 39842107
Great solution - thanks!
0
 

Author Comment

by:DanP1980
ID: 39842127
One too may quotation marks:

Should be

DECLARE @SQL NVARCHAR(2000)
DECLARE @SPName NVARCHAR(50)
DECLARE T CURSOR
FOR
select SUBSTRING(name,4,20) from sysobjects where id in ( select ID from syscomments where ltrim(Rtrim([text])) like '%milking%')
OPEN T
FETCH T INTO @SPName
WHILE @@FETCH_STATUS = 0
BEGIN
 SET @SQL = 'EXEC SP_'  +@SPName
 EXEC (@SQL)
 FETCH T INTO @SPName
END
CLOSE T
DEALLOCATE T
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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