日期:2014-05-17  浏览次数:20654 次

怎样进行“数据回滚”?
怎样进行“数据回滚”?     

一条数据到数据库的处理流程:

1、查询数据配置信息。

2、“数据合法性”和“插入数据历史表”并发执行。

3、计算“合法”数据的“增量”。

4、“插入增量表”、“更新实时数据”、“插入/更新年月日时”表。

问题一:如果“数据合法性”后的“插入增量表”、“更新实时数据”、“插入/更新年月日时”表“执行失败了”,但是“插入数据历史表”执行成功了,这是就会造成“数据的不统一”,怎么办?

问题二:增量计算出来以后,“更新实时数据”成功了,但是“插入增量表”和“插入/更新年月日时”表失败了,怎么办?   怎么保证各个过程之间的数据完整性?


问题三:在并发处理的情况下,怎样保证数据的完整性?

------最佳解决方案--------------------
declare @err int
set @err=0
begin tran
你的数据库语句
select @err=@@error
if @err=0
begin
commit tran
end
else 
begin
rollback tran
end

------其他解决方案--------------------
用银行转账的例子演示吧


 --建表 
     create table card1 (cardid int,remain float)
 create procedure mypro1
 @incount int,
 @outcount int,
 @amount float
 as
  
 begin
 declare @total  float
 select @total=remain   from card1 where cardid=@outcount
 
 
 if @total>=@amount 
 begin
 update card1
 set remain=remain-@amount where cardid=@outcount
 update card1
 set remain=remain+@amount where cardid=@incount
end
 
 end
 insert card1 values(1,1000.0)
 insert card1 values(2,500.0)
 exec mypro1 2,1,200
 select * from card1
sp_help stu
 create procedure mypo2() 
 
 as insert into stu(sno,sname) values('95051','小明')
 go
 exec  mypo2
 select * from stu
 create table card2(cardid int,remain float)
 
 drop procedure mypro2
 create procedure mypro2
 @outid int,
 @inid int,
 @money float
 as 
 begin 
    declare @total float
    select @total=remain from card2 where cardid=@outid
     begin transaction t1--设回滚点
     if @total>@money 
     begin 
       
          update card2
          set remain=remain-@money where cardid=@outid
          
        
          update card2
          set remain=remain+@money where cardid=@inid
          print'转账成功'
          commit transaction t1--提交事务
  
       
         end
            else 
            begin