sql server 常用的事务提交/回滚写法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foren_whb/article/details/84748241

记录备用

BEGIN TRAN
BEGIN TRY  
-- do something as insert or update or delete...
   IF(@@TRANCOUNT>0)                           --全局变量,事务开启值则+1,用来判断是否开启事务
      COMMIT TRAN;
END TRY
BEGIN CATCH
  SELECT  ERROR_NUMBER()    AS ErrorNumber     --错误代码
         ,ERROR_SEVERITY()  AS ErrorSeverity   --错误严重级别,级别小于10捕获不到
         ,ERROR_LINE()      AS ErrorLine       --错误行号
         ,ERROR_STATE()     AS ErrorState      --错误状态码
         ,ERROR_PROCEDURE() AS ErrorProcedure  --出现错误的存储过程或触发器的名称。
         ,ERROR_MESSAGE()   AS ErrorMessage    --错误的具体信息
      IF (@@TRANCOUNT > 0)
          ROLLBACK TRAN;
END CATCH
-- do something as select...

 

没有更多推荐了,返回首页