SQL Server:无法回滚...未找到该名称的事务或保存点

时间:2016-11-02 20:39:17

标签: sql-server transactions

我们有Perl脚本,bcp的各种实体的数据用于分阶段表,然后通过DBI(Autocommit = 0)调用存储过程从Stage-Core表中为这些实体移动数据。

存储过程伪代码如下

   -- GET DETAILS from stage to temproaray tables
  SELECT * FROM <MAIN_STG_TBL> INTO  #MAIN_TBL
  SELECT * FROM <SUPP_STG_1> INTO  #supp_1
  SELECT * FROM <SUPP_STG_2> INTO  #supp_2
  .....
  SELECT * FROM <SUPP_STG_10> INTO  #supp_10


  RAISERROR (<INFO_MSG>, 10, 1) WITH NOWAIT

  -- for each entity in #MAIN_TBL
  -- move datae from stage to core
  WHILE_LOOP ( EACH <ENTRY> in #MAIN_TBL )

    BEGIN TRY

     BEGIN TRAN STG_CORE

        INSERT INTO <MAIN_CORE> SELECT * FROM #MAIN_TBL WHERE ENTITY=<ENTRY>
        INSERT INTO <SUPP_CORE_1> SELECT * FROM #supp_1 WHERE ENTITY=<ENTRY>
        INSERT INTO <SUPP_CORE_1> SELECT * FROM #supp_2 WHERE ENTITY=<ENTRY>
           .....
        INSERT INTO <SUPP_CORE_10> SELECT * FROM #supp_10 WHERE ENTITY=<ENTRY>

     COMMIT TRAN STG_CORE;
    END TRY

    BEGIN CATCH
      ROLLBACK TRAN STG_CORE;
    END CATCH

    RAISERROR (<INFO_MSG>, 10, 1) WITH NOWAIT

  END LOOP

RAISERROR (<INFO_MSG>, 10, 1) WITH NOWAIT

存储过程本身在AutoCommit=0连接中调用,紧接着DBI->commit

问题

对于所有失败的实体200/1000实体,主要是由于Primary Key Violation in some of the SUPP Tables看到了消息STAGE_TO_CORE_SP text=Cannot roll back STG_CORE. No transaction or savepoint of that name was found

0 个答案:

没有答案