Solved

Execute a series of SQL Server 2000 Stored Procedures

Posted on 2008-10-13
2
217 Views
Last Modified: 2012-05-05
Hello,
I have several stored procedures that I need to run in a weekly basis.  What I need is to create a 'master' stored procedure that will have a loop in which the code:

- Gets the name of the stored procedure
- Executes it
- Makes sure it was successful
- Runs the next one.  

And here's the fine print.
I will execute the 'master' stored procedure from a VB.NET Windows application.
The Stored Procedures update records based on criteria specified in said SPs.
I have a table where I have the names of these SPs, so I can select them from it as I loop.  
Also, if one of the stored procedures FAILS for any reason, I need to rollback ALL the transactions done by the previous stored procedures from that list and spit out a message with the name of the offending stored procedure.

Does anyone have a good example of how to accomplish this?

Thanks!


0
Comment
Question by:TheUndecider
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22706872
>I will execute the 'master' stored procedure from a VB.NET Windows application.
sure? I would use a sql server agent job instead...


anyhow:
CREATE PROCEDURE dbo.your_master_prod
AS
BEGIN
DECLARE @err INT
DECLARE c CURSOR FOR SELECT proc_name FROM proc_table ORDER BY execution_order;
DECLARE @proc_name SYSNAME 
BEGIN TRANSACTION 
OPEN c
FETCH NEXT FROM c INTO @proc_name
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC @proc_name
  
  SET @err = @@ERROR
  IF @err <> 0 GOTO DONE 
 
  FETCH NEXT FROM c INTO @proc_name
END 
DONE:
CLOSE c
DEALLOCATE c 
IF @err = 0
  COMMIT
ELSE
  ROLLBACK 
END

Open in new window

0
 

Author Comment

by:TheUndecider
ID: 22712828
Hi Angel,

This looks great!  It's simple and slim.  I do have a question though.  I am not supposed to use Cursors, so.. do you think I could use a select statement instead?  If so, could you please change your code so it reflects this?

Thanks!

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now