SET XACT_ABORT ON
declare @money int,@inputtime datetime,@inputtime2 datetime
declare myCursor CURSOR FOR
select money,inputtime from #a where state=0 order by inputtime
open myCursor
fetch next from myCursor into @money,@inputtime
while @@FETCH_STATUS = 0
begin
select top 1 @inputtime2=inputtime
from #b where money=@money and state=0
order by inputtime asc
if (@inputtime2 is not null)
begin
begin tran
update #b set
state=1
where @inputtime2=inputtime
update #a
set state=1
where @inputtime=inputtime
commit tran
end
fetch next from myCursor into @money, @inputtime
end
close myCursor
deallocate myCursor
SET XACT_ABORT OFF
insert #b
select 100,0,'2003-2-1' union
select 100,0,'2003-2-2' union
select 100,0,'2003-2-3' union
select 100,0,'2003-2-4'
SET XACT_ABORT ON
declare @money int,@inputtime datetime,@inputtime2 datetime
declare myCursor CURSOR FOR
select inputtime from #a where state=0
open myCursor
fetch next from myCursor into @inputtime2
while @@FETCH_STATUS = 0
begin
begin tran
select top 1 @money=money,@inputtime=inputtime from #a where state=0 and inputtime=@inputtime2 order by inputtime
update #a set state=1 where money=@money and inputtime=@inputtime and state=0
if exists(select 1 from #b where money=@money and state=0)
begin
select top 1 @money=money,@inputtime=inputtime from #b where money=@money and state=0 order by inputtime
update #b set state=1 where money=@money and inputtime=@inputtime and state=0
commit tran
end
else
rollback
fetch next from myCursor into @inputtime2
end
close myCursor
deallocate myCursor
SET XACT_ABORT OFF
begin tran
select [money] into #a from a in (select [money] from b where state=0) and state=0
update a set state=1 where [money] in (select [money] from #a)
update b set state=1 where [money] in (select [money] from #a)
commit tran