数据库的回滚

 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