SQL Server TRY CATCH FINALLY

前端 未结 6 2120
执笔经年
执笔经年 2021-02-06 23:09

I have a scenario where I need something similar to .NET\'s try-catch-finally block.

On my try, I will CREATE a #temp table, INSERT

6条回答
  •  长发绾君心
    2021-02-06 23:34

    While not exactly the same as FINALLY, the T-SQL version of Try-Catch does allow that code that needs execute after both the Try and Catch blocks can occur after the end of the END CATCH statement. Using the question code as an example:

        BEGIN TRY
          CREATE TABLE #temp
           (
             --columns
           )
          --Process data with other data sets
        END TRY
        BEGIN CATCH
        EXECUTE usp_getErrorMessage
        END CATCH;
    
    IF OBJECT_ID('tempdb..#temp') IS NOT NULL -- Check for table existence
        DROP TABLE #temp;
    

    The DROP TABLE command will execute whether the Try or Catch execute. See: BOL Try...Catch

提交回复
热议问题