PROBLEMA SQL SERVER ROLLBACK触发器

时间:2012-10-30 22:40:07

标签: sql sql-server

下午好, 我有一个回滚的问题... Treger最初用来调用该过程并且该过程被触发回滚但它是...... 现在我正试图从触发器回滚...

附上代码和错误

感谢您的帮助

USE [Desarrollo_Pruebas]
GO
/****** Object:  Trigger [dbo].[Prueba_tre]    Script Date: 10/30/2012 17:02:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[Prueba_tre] 
   ON  [dbo].[Cliente]
    FOR INSERT
AS 
BEGIN

 SET NOCOUNT ON;

begin try
    BEGIN TRAN t1
    SAVE TRAN t1
    INSERT INTO Articulo (Articulo_Codigo, Articulo_Descripcion, Valor_unitario, Categoria_id, msg) VALUES (1040,'EURECA',15000,5,'')
    COMMIT 
    --raiserror ('Test error',16,1)
end try

begin catch
    ROLLBACK TRAN t1
    SELECT    ERROR_NUMBER() AS ERROR_NUMBER, ERROR_MESSAGE() AS ERROR_MESSAGE;
end catch

--SET NOCOUNT ON;
--exec Prueba CLIENTE_ID;

END

Msg 3931,Level 16,State 1,Procedure Prueba_tre,Line 21 当前事务无法提交,也无法回滚到保存点。回滚整个交易。

表是

我的想法是,当我插入带有插入更新的客户产品时.. 在此事务结束时需要从控制台返回

CREATE TABLE Articulo
(
   Articulo_Codigo int not null primary key
   , Articulo_Descripcion varchar (100)
   , Valor_unitario int
   , Categoria_Id int not null constraint fk_Categoria references Categoria (CATEGORIA_ID)
)

CREATE TABLE Cliente
(
   CLIENTE_ID int not null primary key
   , CLIENTE_PRIMER_NOMBRE varchar(50)
   , CLIENTE_SEGUNDO_NOMBRE varchar(50)
   , CLIENTE_PRIMER_APELLI varchar(50)
   , CLIENTE_SEGUNDO_APELLI varchar(50)
   , CLIENTE_DIRECCION varchar(90)
   , CLIENTE_TELEFONO int
   , CLIENTE_CIUDAD varchar(50)
)

1 个答案:

答案 0 :(得分:0)

继续使用程序。我想你只是不检查异常处理和嵌套事务。

CREATE PROCEDURE [dbo].[Prueba_tre]
AS
BEGIN
  SET NOCOUNT ON;
  declare @trancount int;
  set @trancount = @@trancount;
  begin try
    if @trancount = 0
      begin transaction
    else
      save transaction t1

    INSERT INTO Articulo (Articulo_Codigo, Articulo_Descripcion, Valor_unitario, Categoria_id, msg) VALUES (1040,'EURECA',15000,5,'')
    if @trancount = 0 
      commit; 
  end try
  begin catch
    declare @error int, @message varchar(4000 ), @xstate int;
    select @error = ERROR_NUMBER(),
    @message = ERROR_MESSAGE(),
    @xstate = XACT_STATE();
   if @xstate = -1
     rollback;
   if @xstate = 1 and @trancount = 0
     rollback
   if @xstate = 1 and @trancount > 0
     rollback transaction t1
   raiserror ('t1: %d: %s' , 16, 1, @error, @message)
  end catch
END