存储过程调用存储过程。两个都有事物。
事物嵌套后,执行外部存储过程
提示:
ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION。
-----------------------------
求助
===================下面是示例代码===========================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PROC_A]
@A INT
AS
BEGIN
BEGIN TRAN aa
BEGIN TRY 
DECLARE @BACK_ INT
EXEC @BACK_=[PROC_B] @A--此存储过程也有事物
IF (@BACK_=0)
BEGIN
EXEC @BACK_=[PROC_C] @A--此存储过程也有事物
END
ELSE
BEGIN
SET @BACK_=-1 
ROLLBACK TRAN aa
END
END TRY
BEGIN CATCH
SET @BACK_=4
ROLLBACK TRAN aa
END CATCH
RETURN @BACK_
END
--存储过程B
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PROC_B]
@A INT
AS
BEGIN
BEGIN TRAN
BEGIN TRY 
DECLARE @RE INT
--中间执行操作的代码
END TRY
BEGIN CATCH
SET @RE=4
ROLLBACK TRAN
END CATCH
RETURN @RE
END
--存储过程C
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PROC_C]
@A INT
AS
BEGIN
BEGIN TRAN
BEGIN TRY
  DECLARE @RE INT
--中间执行操作的代码
END TRY
BEGIN CATCH
SET @RE=4
ROLLBACK TRAN
END CATCH
RETURN @RE
END

解决方案 »

  1.   

    [MSSqlServer]SQL Server 中的嵌套事务 
      

  2.   

    你的PROCEDURE [dbo].[PROC_C]和PROCEDURE [dbo].[PROC_B]
    应该没有问题,问题出在A是不是,你单独执行一下试试。
      

  3.   


    ALTER PROCEDURE [dbo].[PROC_A]
        @A INT
    AS
    BEGIN
        BEGIN TRAN aa
        BEGIN TRY 
            DECLARE @BACK_ INT
            EXEC @BACK_=[PROC_B] @A--此存储过程也有事物
            IF (@BACK_=0)
                BEGIN
                    EXEC @BACK_=[PROC_C] @A--此存储过程也有事物
                END
            ELSE
                BEGIN
                    SET @BACK_=-1 
                    commit
                END
        END TRY
        BEGIN CATCH
            SET @BACK_=4
            ROLLBACK TRAN aa
        END CATCH
        RETURN @BACK_
    END
    第一个改成这样试试。
      

  4.   

    第一种方法: 使用output参数USE AdventureWorks;
    GO
    IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL 
        DROP PROCEDURE Production.usp_GetList;
    GO
    CREATE PROCEDURE Production.usp_GetList @product varchar(40) 
        , @maxprice money 
        , @compareprice money OUTPUT
        , @listprice money OUT
    AS
        SELECT p.name AS Product, p.ListPrice AS 'List Price'
        FROM Production.Product p
        JOIN Production.ProductSubcategory s 
          ON p.ProductSubcategoryID = s.ProductSubcategoryID
        WHERE s.name LIKE @product AND p.ListPrice < @maxprice;
    -- Populate the output variable @listprice.
    SET @listprice = (SELECT MAX(p.ListPrice)
            FROM Production.Product p
            JOIN  Production.ProductSubcategory s 
              ON p.ProductSubcategoryID = s.ProductSubcategoryID
            WHERE s.name LIKE @product AND p.ListPrice < @maxprice);
    -- Populate the output variable @compareprice.
    SET @compareprice = @maxprice;
    GO
    另一个存储过程调用的时候:Create Proc Test
    as
    DECLARE @compareprice money, @cost money 
    EXECUTE Production.usp_GetList '%Bikes%', 700, 
        @compareprice OUT, 
        @cost OUTPUT
    IF @cost <= @compareprice 
    BEGIN
        PRINT 'These products can be purchased for less than 
        $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
    END
    ELSE
        PRINT 'The prices for all products in this category exceed 
        $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
    第二种方法:创建一个临时表create proc GetUserName
    as
    begin
        select 'UserName'
    endCreate table #tempTable (userName nvarchar(50))
    insert into #tempTable(userName)
    exec GetUserNameselect #tempTable--用完之后要把临时表清空
    drop table #tempTable--需要注意的是,这种方法不能嵌套。例如:  procedure   a   
      begin   
          ...   
          insert   #table   exec   b   
      end   
        
      procedure   b   
      begin   
          ...   
          insert   #table    exec   c   
          select   *   from   #table     
      end   
        
      procedure   c   
      begin   
          ...   
          select   *   from   sometable   
      end  --这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
    --会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
    第三种方法:声明一个变量,用exec(@sql)执行:1);EXEC 执行SQL语句declare @rsql varchar(250)
            declare @csql varchar(300)
            declare @rc nvarchar(500)
            declare @cstucount int
            declare @ccount int
            set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
            --exec(@rsql)
            set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
            set @rc=@csql+@rsql
            exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法
            --select @csql+@rsql
            --select @cstucount本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx
      

  5.   

    最后自己解决了。感谢楼上各位。
    我的解决方法是。
    给每个存储过程的事物单独定义名称。
    然后回滚的时候回滚指定名称的事物。
    配合return或者output使用。