Solved

Execute a series of SQL Server 2000 Stored Procedures

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 34
sql server query 18 42
Get Next number from Stored Procedure 8 23
SQL - Curser to do an insert based on a select 2 13
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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