Solved

Execute a series of SQL Server 2000 Stored Procedures

Posted on 2008-10-13
2
231 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
[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 Comments
 
LVL 143

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

710 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