Solved

Execute a series of SQL Server 2000 Stored Procedures

Posted on 2008-10-13
2
212 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

863 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

19 Experts available now in Live!

Get 1:1 Help Now