注册 登录
编程论坛 SQL Server论坛

有谁帮我看一下这个存储过程吗,点执行一直处于执行状态!执行了半个小时还在执行

hong293 发布于 2012-07-16 11:48, 832 次点击
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
   
--调整考勤   
ALTER  procedure [dbo].[Proc_UpdateKq]   
@Yearmonth as varchar(7)   
as     
  Declare @FirstDate as smallDatetime      --月初第一天   
  Declare @employeeid as int          --员工ID   
  Declare @RestType as int            --假日类别   
  Declare @checkDate as smalldatetime --考勤日期   
/*   
RestType --假日类型   
OnOffDutyData --考勤档案表   
RestType=6--1-5有一天为 6   
RestType=5--周6更新为5   
*/   
   set @FirstDate=@Yearmonth +'-01'   
   
   Declare Cur Cursor for      
      select Employeeid,checkDate from OnOffDutyData  where Datediff(month,CheckDate,@FirstDate)=0 and RestType=6 and (Datepart(dw,checkdate) between 2 and 6) and employeeid in (select employeeid from employeemsg where department in (select departmentid from datadepartment where preCode='CY')) order by Employeeid,checkDate   
   Open Cur   
     fetch next from cur into @employeeid,@checkDate   
     While (@@Fetch_Status=0)      
       begin      
               
             update OnOffDutyData set  RestType=5,classsouce=3 where employeeid=@employeeid and Datepart(dw,checkdate)=7 and isnull(RestType,0)=0 and (Datepart(wk,checkdate)=Datepart(wk,@checkdate))     
   
                  
       fetch next from cur into @employeeid,@checkDate   
   end            
   
 Close cur        
       Deallocate cur   
   


 



   

5 回复
#2
hong2932012-07-16 12:25
求解啊   
#3
qingshuiliu2012-07-16 16:43
select Employeeid,checkDate from OnOffDutyData  where Datediff(month,CheckDate,@FirstDate)=0 and RestType=6 and (Datepart(dw,checkdate) between 2 and 6) and employeeid in (select employeeid from employeemsg where department in (select departmentid from datadepartment where preCode='CY')) order by Employeeid,checkDate
这一段,你手动执行一下,看看执行情况。

#4
netlin2012-07-17 07:38
3楼的做法是正解。
先把你的存储过程的主体部分拿到查询分析器中运行,调试好后再放回存储过程中。
对于主体部分比较大的,还可以分段进行调试。
如下:

declare @Yearmonth as varchar(7)   
set @Yearmonth='xxxx-xx'   
     
  Declare @FirstDate as smallDatetime      --月初第一天   
  Declare @employeeid as int          --员工ID   
  Declare @RestType as int            --假日类别   
  Declare @checkDate as smalldatetime --考勤日期   
/*   
RestType --假日类型   
OnOffDutyData --考勤档案表   
RestType=6--1-5有一天为 6   
RestType=5--周6更新为5   
*/   
   set @FirstDate=@Yearmonth +'-01'   
   
   Declare Cur Cursor for      
      select Employeeid,checkDate from OnOffDutyData  where Datediff(month,CheckDate,@FirstDate)=0 and RestType=6 and (Datepart(dw,checkdate) between 2 and 6) and employeeid in (select employeeid from employeemsg where department in (select departmentid from datadepartment where preCode='CY')) order by Employeeid,checkDate   
   Open Cur   
     fetch next from cur into @employeeid,@checkDate   
     While (@@Fetch_Status=0)      
       begin      
               
             update OnOffDutyData set  RestType=5,classsouce=3 where employeeid=@employeeid and Datepart(dw,checkdate)=7 and isnull(RestType,0)=0 and (Datepart(wk,checkdate)=Datepart(wk,@checkdate))     
   
                  
       fetch next from cur into @employeeid,@checkDate   
   end      
#5
hong2932012-07-18 10:57
回复 2楼 hong293
你一段没什么问题,执行出来有二千多条记录!
#6
hong2932012-07-18 11:09
回复 3楼 qingshuiliu
谢谢你的指导哦,我在  

 set @FirstDate=@Yearmonth +'-01'   后面插入一个select Employeeid,checkDate from OnOffDutyData  where Datediff(month,CheckDate,@FirstDate)=0 and RestType=6 and (Datepart(dw,checkdate) between 2 and 6) and employeeid in (select employeeid from employeemsg where department in (select departmentid from datadepartment where preCode='CY')) order by Employeeid,checkDate   
到临时表去,执行之后可以了呵呵
1