Solved

Execute a series of SQL Server 2000 Stored Procedures

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

21 Experts available now in Live!

Get 1:1 Help Now