Catch Block with GOTO statement

  • Hi All,

    I have a Stored Proc which populates a table and then uses BCP to output the table into a flat file and lastly ftp the file out to a remote site.

    I'm trying to update the error handling as I first wrote this script on SQL2000 and it has now moved to SQL2008r2. The stored proc looks something like this:

    BEGIN TRY

    BEGIN TRANSACTION

    <A whole bunch of inserts and updates>

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    <Error handling>

    ROLLBACK TRANSACTION

    END CATCH

    BEGIN

    <xp_cmdshell, BCP, FTP stuff>

    END

    What I need to do is jump to the end of the script if an error invokes the CATCH block, so the xp_cmdshell stuff is not exicuted. Can I simply put a GOTO statement to take it to the end in the CATCH block, or do I have to set a variable in the CATCH block then test the variable outside the CATCH block or indeed is there a better way to simply terminate the script following the ROLLBACK?

    Ta

    David

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • dbalmf (3/6/2014)


    Hi All,

    I have a Stored Proc which populates a table and then uses BCP to output the table into a flat file and lastly ftp the file out to a remote site.

    I'm trying to update the error handling as I first wrote this script on SQL2000 and it has now moved to SQL2008r2. The stored proc looks something like this:

    BEGIN TRY

    BEGIN TRANSACTION

    <A whole bunch of inserts and updates>

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    <Error handling>

    ROLLBACK TRANSACTION

    END CATCH

    BEGIN

    <xp_cmdshell, BCP, FTP stuff>

    END

    What I need to do is jump to the end of the script if an error invokes the CATCH block, so the xp_cmdshell stuff is not exicuted. Can I simply put a GOTO statement to take it to the end in the CATCH block, or do I have to set a variable in the CATCH block then test the variable outside the CATCH block or indeed is there a better way to simply terminate the script following the ROLLBACK?

    Ta

    David

    Why not move the xp_cmdshell to before the catch?

  • Hi,

    I have had locking issues with the xp_cmdshell executing within the uncommited transaction so I have moved the catch up the script. I guess I could nest try/catch blocks which might work

    D

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Have you tried moving the <xp_cmdshell, BCP, FTP stuff> after the Commit Transaction?

    Kurt

    Kurt W. Zimmerman
    SR DBA
    Lefrak Organization
    New York, NY

    http://www.linkedin.com/in/kurtwzimmerman

  • This should work for you (create a variable and set the @@ERROR to it, then test for it before you attempt the xp_cmdshell)DECLARE @Err INT

    BEGIN TRY

    BEGIN TRANSACTION

    <A whole bunch of inserts and updates>

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    SET @Err = @@ERROR

    <Error handling>

    ROLLBACK TRANSACTION

    END CATCH

    IF @Err = 0

    BEGIN

    <xp_cmdshell, BCP, FTP stuff>

    END

    ELSE

    BEGIN

    'Do something else or GOTO whereever?'

    END

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • MyDoggieJessie (3/6/2014)


    This should work for you (create a variable and set the @@ERROR to it, then test for it before you attempt the xp_cmdshell)DECLARE @Err INT

    BEGIN TRY

    BEGIN TRANSACTION

    <A whole bunch of inserts and updates>

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    SET @Err = @@ERROR

    <Error handling>

    ROLLBACK TRANSACTION

    END CATCH

    IF @Err = 0

    BEGIN

    <xp_cmdshell, BCP, FTP stuff>

    END

    ELSE

    BEGIN

    'Do something else or GOTO whereever?'

    END

    Three points:

    1) if you're going to test @Err for zero, you need to have set it to 0

    2) don't rely on @@ERROR in a CATCH block, use ERROR_NUMBER() instead

    3) If somehow ERROR_NUMBER() is 0 in the CATCH block, you still want to set @Err to something other than 0

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Absolutely, great catch Scott

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • Thanks everyone, I have taken MyDoggieJessie's idea's with Scot's amendments, it works well and is now live in the prod environment.. 🙂

    Ta

    David

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply