HAREKET (Transact-sql) Kaydet

Bir hareket içinde bir kayıt noktası ayarlar.

Konu bağlantısı simgesi Transact-SQL Sözdizim Kuralları

Sözdizimi

SAVE { TRAN | TRANSACTION } { savepoint_name | @savepoint_variable }
[ ; ]

Bağımsız değişkenler

  • savepoint_name
    Adı için kayıt noktası atanır. Kayıt noktası adlarını tanımlayıcıları kuralları uyması gerekir, ancak 32 karakterle sınırlıdır.

  • @savepoint\_variable
    Kullanıcı tanımlı bir değişken adını içeren bir geçerli kayıt noktası adı. Değişken ile bildirilmeli bir char, varchar, nchar, ya nvarcharveri türü. 32'den fazla karakter değişkeni geçirilebilir, ancak yalnızca ilk 32 karakteri kullanılır.

Açıklamalar

Bir kullanıcı bir kayıt noktası, ya da işaret, bir işlem içinde ayarlayabilirsiniz. Noktası hangi işlemin parçası şartlı iptal hareket dönebilirsiniz konumu tanımlar. Bir hareket için bir kayıt noktası geri alınırsa, tamamlanmasına daha fazla ilerlemek gerekir Transact-SQLgerekirse deyimleri ve TRANSACTION COMMIT deyimi veya gerekir iptal tamamen hareket için başına geri alınıyor. Hareketin iptal etmek için formu rollback TRANSACTION kullanmak transaction_name. Tüm deyimleri veya yordamlar işlem geri alınır.

Yinelenen kayıt noktası adları bir hareket içinde izin verilir, ancak kayıt noktası adı belirten bir rollback TRANSACTION deyimi yalnızca hareket için en son save hareket bu adı kullanarak geri döner.

save TRANSACTION içinde desteklenen dağıtılmış hareketleri ya da açıkça dağıtılmış hareket BEGIN ile başlatıldığında veya yerel bir hareketin escalated.

Önemli notÖnemli

Bir işlem başladığında, hareket sırasında kullanılan kaynakları (yani, kilitler) hareket tamamlama kadar tutulur. Bir hareketin parçası için bir kayıt noktası döndürülüyor kadar işlemin tamamlanması veya bir geri alma tam işlem yapılacak kaynakları devam.

İzinler

Üyelik publicrolü.

Örnekler

Aşağıdaki örnek, yalnızca etkin bir işlem saklı yordam yürütülmeden önce başlatılmışsa saklı yordam tarafından yapılan değişiklikleri geri almak için bir hareket noktası kullanmayı gösterir.

USE AdventureWorks2012;
GO
IF EXISTS (SELECT name FROM sys.objects
           WHERE name = N'SaveTranExample')
    DROP PROCEDURE SaveTranExample;
GO
CREATE PROCEDURE SaveTranExample
    @InputCandidateID INT
AS
    -- Detect whether the procedure was called
    -- from an active transaction and save
    -- that for later use.
    -- In the procedure, @TranCounter = 0
    -- means there was no active transaction
    -- and the procedure started one.
    -- @TranCounter > 0 means an active
    -- transaction was started before the 
    -- procedure was called.
    DECLARE @TranCounter INT;
    SET @TranCounter = @@TRANCOUNT;
    IF @TranCounter > 0
        -- Procedure called when there is
        -- an active transaction.
        -- Create a savepoint to be able
        -- to roll back only the work done
        -- in the procedure if there is an
        -- error.
        SAVE TRANSACTION ProcedureSave;
    ELSE
        -- Procedure must start its own
        -- transaction.
        BEGIN TRANSACTION;
    -- Modify database.
    BEGIN TRY
        DELETE HumanResources.JobCandidate
            WHERE JobCandidateID = @InputCandidateID;
        -- Get here if no errors; must commit
        -- any transaction started in the
        -- procedure, but not commit a transaction
        -- started before the transaction was called.
        IF @TranCounter = 0
            -- @TranCounter = 0 means no transaction was
            -- started before the procedure was called.
            -- The procedure must commit the transaction
            -- it started.
            COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
        -- An error occurred; must determine
        -- which type of rollback will roll
        -- back only the work done in the
        -- procedure.
        IF @TranCounter = 0
            -- Transaction started in procedure.
            -- Roll back complete transaction.
            ROLLBACK TRANSACTION;
        ELSE
            -- Transaction started before procedure
            -- called, do not roll back modifications
            -- made before the procedure was called.
            IF XACT_STATE() <> -1
                -- If the transaction is still valid, just
                -- roll back to the savepoint set at the
                -- start of the stored procedure.
                ROLLBACK TRANSACTION ProcedureSave;
                -- If the transaction is uncommitable, a
                -- rollback to the savepoint is not allowed
                -- because the savepoint rollback writes to
                -- the log. Just return to the caller, which
                -- should roll back the outer transaction.

        -- After the appropriate rollback, echo error
        -- information to the caller.
        DECLARE @ErrorMessage NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState INT;

        SELECT @ErrorMessage = ERROR_MESSAGE();
        SELECT @ErrorSeverity = ERROR_SEVERITY();
        SELECT @ErrorState = ERROR_STATE();

        RAISERROR (@ErrorMessage, -- Message text.
                   @ErrorSeverity, -- Severity.
                   @ErrorState -- State.
                   );
    END CATCH
GO

USE AdventureWorks2012;
GO
IF EXISTS (SELECT name FROM sys.objects
           WHERE name = N'SaveTranExample')
    DROP PROCEDURE SaveTranExample;
GO
CREATE PROCEDURE SaveTranExample
    @InputCandidateID INT
AS
    -- Detect whether the procedure was called
    -- from an active transaction and save
    -- that for later use.
    -- In the procedure, @TranCounter = 0
    -- means there was no active transaction
    -- and the procedure started one.
    -- @TranCounter > 0 means an active
    -- transaction was started before the 
    -- procedure was called.
    DECLARE @TranCounter INT;
    SET @TranCounter = @@TRANCOUNT;
    IF @TranCounter > 0
        -- Procedure called when there is
        -- an active transaction.
        -- Create a savepoint to be able
        -- to roll back only the work done
        -- in the procedure if there is an
        -- error.
        SAVE TRANSACTION ProcedureSave;
    ELSE
        -- Procedure must start its own
        -- transaction.
        BEGIN TRANSACTION;
    -- Modify database.
    BEGIN TRY
        DELETE HumanResources.JobCandidate
            WHERE JobCandidateID = @InputCandidateID;
        -- Get here if no errors; must commit
        -- any transaction started in the
        -- procedure, but not commit a transaction
        -- started before the transaction was called.
        IF @TranCounter = 0
            -- @TranCounter = 0 means no transaction was
            -- started before the procedure was called.
            -- The procedure must commit the transaction
            -- it started.
            COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
        -- An error occurred; must determine
        -- which type of rollback will roll
        -- back only the work done in the
        -- procedure.
        IF @TranCounter = 0
            -- Transaction started in procedure.
            -- Roll back complete transaction.
            ROLLBACK TRANSACTION;
        ELSE
            -- Transaction started before procedure
            -- called, do not roll back modifications
            -- made before the procedure was called.
            IF XACT_STATE() <> -1
                -- If the transaction is still valid, just
                -- roll back to the savepoint set at the
                -- start of the stored procedure.
                ROLLBACK TRANSACTION ProcedureSave;
                -- If the transaction is uncommitable, a
                -- rollback to the savepoint is not allowed
                -- because the savepoint rollback writes to
                -- the log. Just return to the caller, which
                -- should roll back the outer transaction.

        -- After the appropriate rollback, echo error
        -- information to the caller.
        DECLARE @ErrorMessage NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState INT;

        SELECT @ErrorMessage = ERROR_MESSAGE();
        SELECT @ErrorSeverity = ERROR_SEVERITY();
        SELECT @ErrorState = ERROR_STATE();

        RAISERROR (@ErrorMessage, -- Message text.
                   @ErrorSeverity, -- Severity.
                   @ErrorState -- State.
                   );
    END CATCH
GO

Ayrıca bkz.

Başvuru

BEGIN TRANSACTION (Transact-SQL)

TAMAMLAMA hareket (Transact-sql)

COMMIT work (Transact-sql)

ERROR_LINE (Transact-sql)

error_message (Transact-sql)

error_number (Transact-sql)

error_procedure (Transact-sql)

ERROR_SEVERITY (Transact-sql)

error_state (Transact-sql)

RAISERROR (Transact-SQL)

rollback TRANSACTION (Transact-sql)

rollback work (Transact-sql)

DENEYİN...catch (Transact-sql)

xact_state (Transact-sql)