存储过程 改成sql2000能用的 2000不支持row_number over
create proc my_FilteringRecords @time int,@result int output存储 SQL
as
BEGIN
SET NOCOUNT ON;
select *,row_number() over (partition by sn order by arDate) re
into #cu3
from AttendanceRecord where states=0
select a.*
into #cu2
from #cu3 a
inner join #cu3 b on a.sn=b.sn and a.re-b.re=1
where datediff(second,b.arDate,a.arDate)/60>=@time
declare @err int
begin Tran
insert SaveRecord (id,SN,Name,arDate)
select id,SN,Name,arDate from #cu2
union all
select id,SN,Name,arDate from #cu3
where re=1 and sn in (select distinct sn from #cu2)
order by SN,arDate
set @err=@@error
if @err>0
goto label_end
update AttendanceRecord set states=1 where sn in (select distinct sn from #cu3)
set @err=@@error
label_end:
if @err>0
begin
set @result=0
rollback Tran
end
else
begin
set @result=1
commit Tran
end
end
go
[解决办法]
try this,
create proc my_FilteringRecords
(@time int,
@result int output)
as
begin
set nocount on
select a.*,
(select count(1)
from AttendanceRecord b
where b.states=a.states and b.sn=a.sn and b.arDate<=a.arDate) 're'
into #cu3
from AttendanceRecord a
where a.states=0
select a.*
into #cu2
from #cu3 a
inner join #cu3 b on a.sn=b.sn and a.re-b.re=1
where datediff(second,b.arDate,a.arDate)/60>=@time
declare @err int
begin Tran
insert SaveRecord (id,SN,Name,arDate)
select id,SN,Name,arDate from #cu2
union all
select id,SN,Name,arDate from #cu3
where re=1 and sn in (select distinct sn from #cu2)
order by SN,arDate
set @err=@@error
if @err>0
goto label_end
update AttendanceRecord set states=1 where sn in (select distinct sn from #cu3)
set @err=@@error
label_end:
if @err>0
begin
set @result=0
rollback Tran
end
else
begin
set @result=1
commit Tran
end
end