SQL Server中while循环中的事务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server中while循环中的事务相关的知识,希望对你有一定的参考价值。

回滚时发生错误:

消息6401,级别16,状态1,过程our_trigger,第76行 无法回滚t1。未找到该名称的任何事务或保存点。

这是触发器中的SQL代码

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [DOCSADM].[our_trigger]
ON [DOCSADM].[PROFILE]
FOR UPDATE, INSERT
AS
    DECLARE @DocSystemId AS INTEGER
    DECLARE @docNumber AS INTEGER
    DECLARE @lastUsedSystemId AS INTEGER
    DECLARE @itemType AS VARCHAR(1) --dm10 
    DECLARE @manualBarcode AS VARCHAR (50)
    DECLARE @oldBarcodeFK AS INTEGER
    --user typed barcode, it must be inserted/UPDATED into pd_barcode table
    DECLARE @COUNTEXISTINGBARCODE AS INTEGER

    SET nocount ON;

    BEGIN
        DECLARE activity_cursor CURSOR local FOR
            SELECT 
                system_id, docnumber,
                a_doc_barcode, pd_doc_barcode, item_type
            FROM  
                inserted

        --find the last used systemid  
        SELECT @lastUsedSystemId = lastkey
        FROM docsadm.seq_systemkey

        OPEN activity_cursor

        FETCH next FROM activity_cursor INTO @DocSystemId, @docNumber, @manualBarcode, @oldBarcodeFK, @itemtype

        WHILE (@@fetch_status <> -1)
        BEGIN
            IF (( @itemType = 'M' OR @itemType = 'P' ))
                --FIND IF IT EXISTS ALREADY A BARCODE
                SELECT @COUNTEXISTINGBARCODE = COUNT(*)
                FROM docsadm.pd_barcode
                WHERE pd_barcode = @manualBarcode

                IF (@COUNTEXISTINGBARCODE = 0)-- THERE IS NO EXISTING BARCODE
                     DECLARE @barcodeSystemId AS INTEGER = 0

                BEGIN TRANSACTION t1
                BEGIN TRY
                    -- get next sys id    
                    EXECUTE [DOCSADM].[Sp_nextkey] 'SYSTEMKEY'

                    SELECT @barcodeSystemId = lastkey
                    FROM docsadm.seq_systemkey

                    INSERT INTO docsadm.pd_barcode
                    VALUES (@manualBarcode, @barcodeSystemId, 'D', NULL, NULL, 'Y', NULL, NULL)

                    UPDATE docsadm.profile
                    SET pd_doc_barcode = @barcodeSystemId
                    WHERE docnumber = @docNumber

                    COMMIT TRANSACTION t1
                END TRY
                BEGIN CATCH
                    ROLLBACK TRANSACTION t1
                END CATCH
            END

            IF (@COUNTEXISTINGBARCODE <> 0)
               --YES THERE IS AT LEAST ONE BARCODE
            BEGIN
                SELECT TOP 1 @barcodeSystemId = system_id
                FROM docsadm.pd_barcode
                WHERE pd_barcode = @manualBarcode

                BEGIN TRANSACTION t1
                BEGIN TRY
                    --update profile's new barcode reference
                    UPDATE docsadm.profile
                    SET pd_doc_barcode = @barcodeSystemId
                    WHERE docnumber = @docNumber

                    UPDATE docsadm.pd_barcode
                    SET pd_doc_bcode_used = 'Y'
                    WHERE system_id = @barcodeSystemId

                    IF (@oldBarcodeFK <> 0)
                    BEGIN
                        --update old barcode as not used!
                        UPDATE docsadm.pd_barcode
                        SET pd_doc_bcode_used = 'N'
                        WHERE system_id = @oldBarcodeFK
                    END

                    COMMIT TRANSACTION t1
                END TRY
                BEGIN CATCH
                    ROLLBACK TRANSACTION t1
                END CATCH
                END

                FETCH next FROM activity_cursor INTO @DocSystemId, @docNumber, @manualBarcode, @oldBarcodeFK, @itemtype
            END

            CLOSE activity_cursor
            DEALLOCATE activity_cursor
      END

错误来自这里

  BEGIN CATCH
      ROLLBACK TRANSACTION t1
  END CATCH

我试图保存trans但我得到了同样的错误。我也在循环之前开始转换但是消息发生了。

答案

此错误表示您尝试提交或回滚不存在的事务。因此,在ROLLBACK之前检查是否存在未提交的事务。像这样

IF @@TRANCOUNT>0
ROLLBACK TRANSACTION

以上是关于SQL Server中while循环中的事务的主要内容,如果未能解决你的问题,请参考以下文章

避免 SQL Server 中的 while 循环

while循环内的表变量每次都没有初始化:SQL Server

带有相关子查询的 While 循环的 SQL Server 性能调整

sql server中的while循环语句

SQL Server 2008 中的 While 循环遍历日期范围,然后插入

Sql server 游标与 While 循环的性能