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

MS SQL的Save Tran例子        
-- After the appropriate rollback, echo error
MS SQL的Save Tran例子
        -- information to the caller.
MS SQL的Save Tran例子
        DECLARE @ErrorMessage NVARCHAR(4000);
MS SQL的Save Tran例子        
DECLARE @ErrorSeverity INT;
MS SQL的Save Tran例子        
DECLARE @ErrorState INT;
MS SQL的Save Tran例子
MS SQL的Save Tran例子        
SELECT @ErrorMessage = ERROR_MESSAGE();
MS SQL的Save Tran例子        
SELECT @ErrorSeverity = ERROR_SEVERITY();
MS SQL的Save Tran例子        
SELECT @ErrorState = ERROR_STATE();
MS SQL的Save Tran例子
MS SQL的Save Tran例子        
RAISERROR (@ErrorMessage-- Message text.
MS SQL的Save Tran例子
                   @ErrorSeverity-- Severity.
MS SQL的Save Tran例子
                   @ErrorState -- State.
MS SQL的Save Tran例子
                   );
MS SQL的Save Tran例子    
END CATCH
MS SQL的Save Tran例子
GO
MS SQL的Save Tran例子

相关文章:

  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
猜你喜欢
  • 2022-01-21
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案