Nested stored procedures containing TRY CATCH ROLLBACK pattern?

后端 未结 5 2079

I\'m interested in the side effects and potential problems of the following pattern:

CREATE PROCEDURE [Name]
AS
BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
            


        
5条回答
  •  悲哀的现实
    2020-11-22 10:55

    -- @Amanda method above doesnt return correct error number

    DECLARE  
      @ErrorMessage   nvarchar(4000),  
      @ErrorSeverity   int,  
      @ErrorState int,  
      @ErrorLine  int,  
      @ErrorNumber   int  
    
    BEGIN TRY  
     SELECT 1/0; -- CATCH me  
    END TRY  
    
    BEGIN CATCH  
    
      DECLARE @err int = @@ERROR  
    
      PRINT @err           -- 8134, divide by zero  
      PRINT ERROR_NUMBER() -- 8134  
    
      SELECT  
        @ErrorMessage  = ERROR_MESSAGE(),  
        @ErrorSeverity = ERROR_SEVERITY(),  
        @ErrorState    = ERROR_STATE(),  
        @ErrorNumber   = ERROR_NUMBER(),  
        @ErrorLine     = ERROR_LINE()  
    
      -- error number = 50000 :(  
      RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorNumber, @ErrorLine)  
    
    END CATCH  
    
    -- error number = 8134  
    SELECT 1/0
    

提交回复
热议问题