if object_id('P_EMRealMonitor_test')is not null
 drop proc P_EMRealMonitor_test
go
create procedure [P_EMRealMonitor_test]  as
SET NOCOUNT On
begin 
declare @ChildID int  while @@ROWCOUNT > 0--这里第一次执行时,没有返回受上一语句影响的行数,所以下面语句也不能执行,不知道大家有什么好方法吗? begin

select top 1 @ChildID=b.ChildiD from EMRealMonitor b where IsCheck=0 order by b.yymmdd desc ,b.cardtime desc 
       update EMRealMonitor set IsCheck=1 where ChildiD=@ChildID

  if exists (

 select top 1 a.yymmdd,a.cardTime,a.mid,a.empid from EMRealMonitor a ,
(select  b.yymmdd,b.cardTime,b.mid,b.empid from EMRealMonitor b where b.ChildiD=@ChildID)c,
(select mid1,mid2,intervaltiem from EMRealMonitor_test) d  
where a.empid=c.empid and c.mid=d.mid2 
and  
DATEDIFF(ss,case when a.yymmdd<c.yymmdd then dateadd(hh,-24,a.cardTime) else a.cardtime end,c.cardTime)>0--d.intervaltiem*60--考虑隔天打卡时

order by a.cardtime desc
  )
  begin  
    update EMRealMonitor set IsExcept=1 where ChildID=@ChildID
  end
         
  end
end