zoukankan      html  css  js  c++  java
  • SQL SERVER 事务处理通用模板

    -- =============================================
    -- Transaction Temp
    -- =============================================
    BEGIN TRY
        BEGIN TRANSACTION
     
        --
        -- You code here.
        --
     
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
    
        IF (@@TRANCOUNT > 0)
            -- Adds store procedure
            -- Writes the error into ErrorLog table.
            ROLLBACK TRANSACTION
        END IF
    END CATCH

    例如:

    BEGIN TRY
            BEGIN TRANSACTION
                    PRINT
    'In [SPMultiDataToUserInfo] Transactions: ' + Convert(varchar, @@TRANCOUNT);
                   
    -- SET NOCOUNT ON added to prevent extra result sets from
                    -- interfering with SELECT statements.
                    --SET NOCOUNT ON;

                    -- Hard code inserted data.
                   
    INSERT INTO UserInfo VALUES('Cris', 1);
                   
    EXEC SPAddDataToUserInfo
                    INSERT INTO UserInfo VALUES('Ada', 32);

           
    COMMIT TRANSACTION

        END TRY
        BEGIN CATCH
            PRINT
    'Error in [SPMultiDataToUserInfo]: ' + ERROR_MESSAGE();
           
    IF (@@TRANCOUNT > 0)
               
    ROLLBACK TRANSACTION
            PRINT
    'Rolled back successful in SPMultiDataToUserInfo Transactions: ' + Convert(varchar, @@TRANCOUNT);
       
    END CATCH
       
    END

  • 相关阅读:
    Mac014--Sourcetree安装(Git client)
    SSM003/构建Maven单模块项目(二)
    Git016--Work
    Mac013--Docker安装
    前端002/常用标签属性(工作应用)
    Python 38 初识数据库
    Python 38 sql基础
    Python 39 数据库的数据类型
    Python 39 数据库
    Python 37 进程池与线程池 、 协程
  • 原文地址:https://www.cnblogs.com/huanghai223/p/2284436.html
Copyright © 2011-2022 走看看