if object_id(‘##tmp1‘) is not null drop table #tmp1 select staffno, paydate into #tmp1 from dbo.OSS_ClockingDetail where paydate>getdate()-50 and ProcessInd=‘P‘ and ActionBy=‘D‘ and paymonth is null group by staffno,paydate declare @staffno varchar(6) declare @paydate datetime declare cur cursor for select staffno, paydate from #tmp1 open cur fetch next from cur into @staffno,@paydate WHILE (@@FETCH_STATUS = 0) begin update dbo.OSS_Rawclocking set intime=‘‘,outtime=‘‘,payhour=0,offind=0 where paydate=@paydate and staffno=@staffno update dbo.OSS_ClockingDetail set ProcessInd=‘U‘ where paydate=@paydate and ProcessInd=‘P‘ and ActionBy=‘D‘ and paymonth is null and staffno=@staffno fetch next from cur into @staffno,@paydate end close cur DEALLOCATE cur