zoukankan      html  css  js  c++  java
  • SQLServer 在存储过程里使用事务控制的简单小例子

    alter proc sp_test
    (
         @name varchar(50)
    )
    as
    begin
       --开始事务
       begin transaction
       --设置一个存储报错代码的变量
       DECLARE @errorSum INT
       SET @errorSum=0
      
       --在每一条涉及到数据变动的语句后都累加一次报错代码

       insert into benInfo values(117,111,'111',111,2)
       SET @errorSum+=@@ERROR
      
       update benInfo set benMoney=222222222 where personID=10
       SET @errorSum+=@@ERROR
       
       --报错代码不为0表示在执行上述sql时出现错误,回滚事务,撤销所有更改
       IF ( @errorSum <> 0 )
       BEGIN
            print '出错,回滚'+cast(@errorSum as varchar(50))
            ROLLBACK TRANSACTION
       END
       ELSE
       --报错代码为0表示无报错信息,可以执行更改操作
       BEGIN
            print '提交'+cast(@errorSum as varchar(50))
            commit transaction
       END

      
    end

    --select * from benInfo

    --exec sp_test ''

     

  • 相关阅读:
    idea database testconnection 显示灰色
    idea tomcat热部署
    idea 常见报错问题 记录
    Python-Basis-22th
    Python-Basis-21th
    Python-Basis-20th
    Python-Basis-19th
    Python-Basis-18th
    Python-Basis-17th
    Python-Basis-16th
  • 原文地址:https://www.cnblogs.com/xcxcxcxc/p/5541185.html
Copyright © 2011-2022 走看看