数据库的回滚

 1   begin try    
 2   begin transaction --开始事务     
 3   begin
 4     insert into Table_A (columnabc values ('1','2')
 5   end
 6   commit transaction --提交事务
 7   end try 
 8   begin catch --获取错误
 9     --select ERROR_NUMBER() as errornumber --错误原因
10     --SELECT ERROR_NUMBER() AS 错误号,  
11     --ERROR_SEVERITY() AS 错误等级,  
12     --ERROR_STATE() as 错误状态,  
13     --DB_ID() as 数据库ID,  
14     --DB_NAME() as 数据库名称,  
15     --ERROR_MESSAGE() as 错误信息;
16     --rollback transaction 
17   end catch
 1     --transaction demo
 2     create procedure  [dbo].[xxx] as
 3     begin transaction
 4     declare @error int 
 5         select @error =0
 6         --insert into xxx...
 7         select @error = @error+@@ERROR
 8         --update xxx set xxx...
 9         select @error = @error+@@ERROR
10         --delete from xxx...
11         select @error = @error+@@ERROR
12     if @@ERROR=0
13     commit transaction
14     else 
15     rollback transaction

 切记,truncate语句不会回滚。

转载于:https://www.cnblogs.com/qingyanxiaochen/p/5160983.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值