views:

48

answers:

1

so I created that(I used some stuff found on other website) to handle transactions and having a sort of stacktrace while executing stored procedure that could call other stored procedure that need transaction and etc.

so if I have A calling B and B is calling C and C got an error, I can correctly rollback my stuff and returning a stacktrace saying: error in C follow the trace to find out where/how/etc...

do any of you find a problem with this logic?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[NAME]
AS
BEGIN
 SET NOCOUNT ON
 SET XACT_ABORT ON

 declare @trancount int
 set @trancount = @@trancount
 declare @savePointName varchar(40)
 set @savePointName = newid()

 BEGIN TRY  
  if @trancount = 0
   begin transaction
  else
   save transaction @savePointName

  /*
  //  STUFF HERE
  */

  if @trancount = 0
   commit transaction
 END TRY  
 BEGIN CATCH
  declare  @xstate int
  set @xstate = XACT_STATE()
  if @xstate = -1  and @trancount = 0
   rollback transaction
  if @xstate = 1 and @trancount = 0
   rollback transaction
  if @xstate = 1 and @trancount > 0
   rollback transaction @savePointName

  declare @message varchar(max)
   set @message = ERROR_MESSAGE() + 
           ' (' + ERROR_PROCEDURE() + 
           ':' + ltrim(str(ERROR_LINE())) + 
           ', Raised ' + ltrim(str(ERROR_NUMBER())) +
           ', Severity ' + ltrim(str(ERROR_SEVERITY())) +
           ', State ' + ltrim(str(ERROR_STATE())) + ')'

  RAISERROR(@message,16,1)
 END CATCH
END
+1  A: 

No, I can't spot anything wrong with this code.

Rubens Farias