Results 1 to 4 of 4

Thread: Stored Procedure Execution of Multiple Scripts

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Stored Procedure Execution of Multiple Scripts

    Hi, I'm not sure what terms to search so I decided to post it as a new thread.

    I have a stored procedure with multiple 'update' scripts to multiple tables. Sometimes I do get unexpected errors with one or more of the scripts, but when this scenario happens, all the other scripts run smoothly. How can I prevent this from happening? I want all the scripts execute sequentially and if there is an error in one or more of the scripts then the stored procedure shouldn't run at all.

    Thank in advance.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Stored Procedure Execution of Multiple Scripts

    What kind of scrips are you talking about ?

    Why don't you start a transaction at the beginning of the procedure, and roll back if you get any kind of error ?

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Stored Procedure Execution of Multiple Scripts

    You need to do aggresive tracking of @@ERROR and handle that with ROLLBACK or COMMIT at the end of the SPROC's.

    Please show us a bit of your SPROC code so we can assist you with how that would be done.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Stored Procedure Execution of Multiple Scripts

    If you don't want to check the @@ERROR every single time you do an insert/update, then you can do it more easier like this:

    sql Code:
    1. SET XACT_ABORT ON
    2. BEGIN TRANSACTION
    3.  
    4. INSERT INTO a (b,c) VALUES('aa', 'bb')
    5. INSERT INTO a (b,c) VALUES('aa', 'bb')
    6. INSERT INTO a (b,c) VALUES('cc', 'hh')
    7.  
    8. COMMIT TRANSACTION
    It explains about it here: SET XACT_ABORT (Transact-SQL)

    It will couse to roll back the entire transaction, when there is any kind of error...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width