Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 3, current count = 4

0

In the following procedure stored in sql server 2012, I get the following error when I execute it with the statement:

EXEC DBRepStage.dbo.spDesbloquear '172011235114','2016-03-03 09:32:52.000','xxxx','03/03/2016 11:15:53'

The result is:

  

Result Message   ----------- --------------------- 0 The bit is not active

     

Msg 266, Level 16, State 2, Procedure sp Unlock, Line 0   Transaction count after EXECUTE indicates a mismatching number of   BEGIN and COMMIT statements. Previous count = 4, current count = 5.

USE DBSucursal
GO


alter PROCEDURE spDesbloquear
@pecCodCta          as VARCHAR(12),
@pecFechaHoraReg    as varchar(30),
@pecUsuMod          as char(4),
@pecFechaHoraMod    as varchar(30)
AS BEGIN
----------------------------------------------------------------    
BEGIN TRY       
        set nocount on;

        DECLARE @vcMensaje      VARCHAR(1800);
        DECLARE @vbActivacion   bit;
        DECLARE @vcSql          nVARCHAR(3500);
        DECLARE @vcParametros as nvarchar(500);
        -----------------------------------------------------------------------------
        BEGIN TRAN MiTransaccion
        ------------------------------------------------------------------------------
                -- Obtener el bit de activación del producto
                SET @vcSql = '';
                SET @vcSql = @vcSql + 'SELECT   @cValorVarOUT = cValorVar' + char(10);
                SET @vcSql = @vcSql + 'FROM     DBCmact' + SUBSTRING(@pecCodCta,1,2) + '..VarSistema AS V ' + char(10);
                SET @vcSql = @vcSql + 'WHERE    V.cNomVar = ''bBitCheckListEnvioCorreo''' + char(10);   
                SET @vcParametros = N'@cValorVarOUT Char(1) OUTPUT';

                EXECUTE sp_executesql @vcSql, @vcParametros,@cValorVarOUT = @vbActivacion OUTPUT;   
                ------------------------------------------------------------------------------
                SET @vbActivacion = ISNULL(@vbActivacion,0)

                IF @vbActivacion = 0
                BEGIN
                        SELECT  0 AS Resultado,'El bit no está activo' as Mensaje
                        RETURN
                END         
                ----------------------------------------------------------------
                INSERT INTO DBSucursal..BloqueosHisto-- DesembBloqueoProximoHisto
                (nIndice,cCodCta,cCodPers,cAnalista,nEstado,dFecReg,cUsuReg,dFecMod,cUsuMod)
                SELECT  nIndice,cCodCta,cCodPers,cAnalista,nEstado,dFecReg,cUsuReg,dFecMod,cUsuMod
                FROM    DBSucursal..DesembBloqueoProximo
                WHERE   cCodCta = @pecCodCta
                AND     nEstado in (10,20)
                AND     dFecReg = @pecFechaHoraReg
                ----------------------------------------------------------------
                UPDATE  DBSucursal..Bloqueos
                SET     nEstado = 60,
                        dFecMod = @pecFechaHoraMod,
                        cUsuMod = @pecUsuMod
                WHERE   cCodCta = @pecCodCta
                AND     nEstado in (10,20)
                AND     dFecReg = @pecFechaHoraReg
                ----------------------------------------------------------------
        COMMIT TRAN MiTransaccion

                SELECT  1 AS Resultado,'' as Mensaje
END TRY 
BEGIN CATCH     
        ROLLBACK TRAN MiTransaccion
        SET @vcMensaje = cast(ERROR_PROCEDURE() as varchar(50)) + CHAR(10) + cast(ERROR_MESSAGE() as varchar(1500));        
        SELECT  -1 AS Resultado,@vcMensaje as Mensaje

END CATCH       
END
    
asked by Dibe 18.05.2016 в 19:16
source

2 answers

1

There is a return without the corresponding closing of the transaction

The complete code of the sp would be the following assuming that in that return it is going to make a Rollback

USE DBSucursal
GO


alter PROCEDURE spDesbloquear
@pecCodCta          as VARCHAR(12),
@pecFechaHoraReg    as varchar(30),
@pecUsuMod          as char(4),
@pecFechaHoraMod    as varchar(30)
AS BEGIN
----------------------------------------------------------------    
BEGIN TRY       
        set nocount on;

        DECLARE @vcMensaje      VARCHAR(1800);
        DECLARE @vbActivacion   bit;
        DECLARE @vcSql          nVARCHAR(3500);
        DECLARE @vcParametros as nvarchar(500);
        -----------------------------------------------------------------------------
        BEGIN TRAN MiTransaccion
        ------------------------------------------------------------------------------
                -- Obtener el bit de activación del producto
                SET @vcSql = '';
                SET @vcSql = @vcSql + 'SELECT   @cValorVarOUT = cValorVar' + char(10);
                SET @vcSql = @vcSql + 'FROM     DBCmact' + SUBSTRING(@pecCodCta,1,2) + '..VarSistema AS V ' + char(10);
                SET @vcSql = @vcSql + 'WHERE    V.cNomVar = ''bBitCheckListEnvioCorreo''' + char(10);   
                SET @vcParametros = N'@cValorVarOUT Char(1) OUTPUT';

                EXECUTE sp_executesql @vcSql, @vcParametros,@cValorVarOUT = @vbActivacion OUTPUT;   
                ------------------------------------------------------------------------------
                SET @vbActivacion = ISNULL(@vbActivacion,0)

                IF @vbActivacion = 0
                BEGIN
                        SELECT  0 AS Resultado,'El bit no está activo' as Mensaje
                        ROLLBACK TRAN 
                        RETURN
                END         
                ----------------------------------------------------------------
                INSERT INTO DBSucursal..BloqueosHisto-- DesembBloqueoProximoHisto
                (nIndice,cCodCta,cCodPers,cAnalista,nEstado,dFecReg,cUsuReg,dFecMod,cUsuMod)
                SELECT  nIndice,cCodCta,cCodPers,cAnalista,nEstado,dFecReg,cUsuReg,dFecMod,cUsuMod
                FROM    DBSucursal..DesembBloqueoProximo
                WHERE   cCodCta = @pecCodCta
                AND     nEstado in (10,20)
                AND     dFecReg = @pecFechaHoraReg
                ----------------------------------------------------------------
                UPDATE  DBSucursal..Bloqueos
                SET     nEstado = 60,
                        dFecMod = @pecFechaHoraMod,
                        cUsuMod = @pecUsuMod
                WHERE   cCodCta = @pecCodCta
                AND     nEstado in (10,20)
                AND     dFecReg = @pecFechaHoraReg
                ----------------------------------------------------------------
                COMMIT TRAN MiTransaccion

                SELECT  1 AS Resultado,'' as Mensaje
END TRY 
BEGIN CATCH     
        ROLLBACK TRAN MiTransaccion
        SET @vcMensaje = cast(ERROR_PROCEDURE() as varchar(50)) + CHAR(10) + cast(ERROR_MESSAGE() as varchar(1500));        
        SELECT  -1 AS Resultado,@vcMensaje as Mensaje

END CATCH       
END
    
answered by 20.05.2016 в 22:08
0
/*El problema es que realizas el RETURN con una transacción abierta.
  Debes realizar el commit o rollback para terminar la transacción
  y salir del proceso*/


IF @vbActivacion = 0
BEGIN
 SELECT  0 AS Resultado,'El bit no está activo' as Mensaje
 COMMIT TRAN MiTransaccion -- o bien el rollback  
 RETURN
END    
    
answered by 20.05.2016 в 15:02