controlar error en transacción
Tengo el siguiente proceso almacenado, que cuando falla en su grabación igual arroja ÉXITO (@@ERROR=0) y hace COMMIT, aunque no graba nada.
La preguntonta es: ¿Por qué @@ERROR no refleja la realidad?
Según entendí en la ayuda dice que @@ERROR es global, ¿esto es realmente así?
¿Alguien conoce otro método de controlar los errores de los procedimientos almacenados?
Desde ya agradezco la ayuda que me puedan dar.
declare @IDCompr int,@TipoCompr varchar(4),@er int
Set @IDCompr = 144
Set @TipoCompr = 'FC'
set @er = 0
begin transaction
Exec Detalle @IDCompr, @TipoCompr
set @er = @@ERROR
IF (@er<>0)
begin
rollback
print 'falló'
print @er
end
else
begin
commit
print 'exito'
print @er
end
Servidor: mensaje 515, nivel 16, estado 2, procedimiento Detalle, línea 13
No se puede insertar el valor NULL en la columna 'Codigo', tabla 'LASEGUNDA.dbo.Factura'.
La columna no admite valores NULL. INSERT falla.
Se terminó la instrucción.
exito
0
CREATE PROCEDURE Detalle @IdComprobante int , @tipoDetalle char(4) AS
DECLARE @Cantidad int , @Detalle varchar(50) , @PrecioUnit real , @Descuento real , @Importe real
DECLARE HacerDetalle cursor for
select Cantidad, Detalle , PrecioUnit ,Descuento, Importe
from TemporalItems
Open HacerDetalle
fetch next from HacerDetalle INTO @Cantidad , @Detalle , @PrecioUnit, @Descuento , @Importe
While (@@Fetch_status = 0)
begin
if @tipoDetalle = 'FC' insert into Factura (idComprobante , Cantidad , Detalle , PrecioUnit , Bonif , total )
values ( @idComprobante , @Cantidad , @Detalle , @PrecioUnit, @Descuento , @Importe )
if @tipoDetalle = 'NC' insert into NCredito (idComprobante , Cantidad , Detalle , PrecioUnit , Bonif , total )
values (@idComprobante ,@Cantidad , @Detalle , @PrecioUnit, @Descuento , @Importe )
if @tipoDetalle = 'ND' insert into NDebito (idComprobante , Cantidad , Detalle , PrecioUnit , Bonif , total )
values (@idComprobante , @Cantidad , @Detalle , @PrecioUnit, @Descuento , @Importe )
fetch next from HacerDetalle INTO @Cantidad , @Detalle , @PrecioUnit, @Descuento , @Importe
end
Close HacerDetalle
Deallocate HacerDetalle
GO
|