|  | 网站首页 | .Net研究 | 
您现在的位置: 程序员之路 >> .Net研究 >> .net应用技术 >> 企业信息化 >> 文章正文
赞助商链接
频 道 导 航
.Net研究频道栏目导航
相 关 文 章
FQYZjiaozhigong考勤排班(利用外部文件导入生成)           
FQYZjiaozhigong考勤排班(利用外部文件导入生成)
作者:cyz1980 文章来源:不详 更新时间:2007-5-10 11:41:11

FQYZjiaozhigong考勤排班(利用外部文件导入生成)

                                   --FQYZjiaozhigong考勤排班(利用外部文件导入生成)
declare @id int,@name varchar(30),@Start char(8),@End char(8),@col sysname,@s varchar(8000),@custid int,@s_col nvarchar(255)--,@h_col varchar(8000)
declare @st datetime, /*排班起始日期*/@et datetime /*排班终止日期*/,@dept varchar(10) ,@planid int,@i int --作为planworkid值这用
declare @t datetime,@procdt datetime
set @st='2007-1-1'
set @et='2007-1-7'
CREATE TABLE #dt (
 [dt] [datetime] NOT NULL
)

while  (select  datediff(day,@st,@et))>=0
  begin
    insert into #dt values(@st)
    select  @st=dateadd(day,1,@st)
  end
select DATEADD(day,  DATEDIFF(day,0,#dt.dt),  0)  as d,datepart(weekday,#dt.dt)  as weekid into #d_w from #dt
/*select @h_col=''
select @h_col=@h_col+name+',' from bc
select @h_col=substring(ltrim(rtrim(@h_col)),1,len(ltrim(rtrim(@h_col)))-1) */
declare bc cursor for select * from bc order by id
open bc
fetch next from bc into @id,@name,@Start,@End
while (@@fetch_status=0)
begin
  declare col cursor for SELECT syscolumns.name FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE (sysobjects.name = 'exam')
  and syscolumns.name=@name
  open col
  fetch next from col into @col
 
  while (@@fetch_status=0)
  begin 
  
    select @s='declare exam cursor for SELECT custid,'+@col+ '  FROM Exam' 
    exec(@s)
    open exam
    fetch next from exam into @custid,@s_col
    
    while (@@fetch_status=0)
    begin
      /*处理开始:*/
    
      if (@s_col is null or @s_col='')
        begin
         select null
        end
      else
        begin
         SELECT @dept=DeptCode  FROM Customers WHERE (CustID =@custid )

         select d,rtrim(cast(year(d) as char))+'-'+rtrim(cast(month(d) as char))+'-'+rtrim(cast(day(d) as char))+' '+@Start as p1,
         rtrim(cast(year(d) as char))+'-'+rtrim(cast(month(d) as char))+'-'+rtrim(cast(day(d) as char))+' '+ @End as p2 into #d_t
         from #d_w where  weekid in (select * from f_h(@s_col,',') as a )
         
         insert into  ProcRecords(CustID, ProcDT, PlanWorkID, PlanStart, PlanEnd, DeptCode)
         select @custid ,d,0 as planid,p1,p2,@dept from #d_t     
        
         
         drop table #d_t 
        end
      /*处理结束*/
      fetch next from exam into @custid,@s_col
    end
    close exam
    deallocate exam


  
    fetch next from col into @col
  end
  close col
  deallocate col
  fetch next from bc into @id,@name,@Start,@End
end
close bc
deallocate bc
/*planworkid值处理开始:*/
declare custid cursor for select distinct custid,procdt from ProcRecords where ProcDT between (select min(d) from #d_w) and (select max(d) from #d_w)
open custid
fetch next from custid into @custid,@procdt
while (@@fetch_status=0)
begin
  select @i=1
  declare planid cursor for select planworkid,PlanStart from ProcRecords where custid=@custid and procdt=@procdt and ProcDT between (select min(d) from #d_w) and (select max(d) from #d_w) order by PlanStart
  open planid
  fetch next from planid into @planid,@t
  while (@@fetch_status=0)
  begin
    update ProcRecords set planworkid=@i where custid=@custid and PlanStart=@t
    select @i=@i+1
    fetch next from planid into @planid,@t
  end
  close planid
  deallocate planid
 
  fetch next from custid into @custid,@procdt
end
close custid
deallocate custid
/*planworkid值处理结束*/
drop table #d_w
drop table #dt

------------------(以下为bc表的触发器,主要作用是根据bc表的name字段值自动增减改Exam表相应的字段)

CREATE  TRIGGER [Tr] ON [dbo].[bc]
FOR INSERT, UPDATE, DELETE
AS
declare @n varchar(100),@o varchar(100)
if @@rowcount>0
  begin
    if exists(select i.name from  inserted i where i.id<>all(select d.id from deleted d))
      begin
        select @n=i.name from  inserted i where i.id<>all(select d.id from deleted d) --新增列
        exec('alter table Exam add '+@n+ ' varchar(255) null')      
      end
    if exists(select d.name from deleted d where d.id<>all(select i.id from inserted i))
      begin
        select @n=d.name from deleted d where d.id<>all(select i.id from inserted i) --减少列
        exec('ALTER TABLE Exam DROP COLUMN '+@n)
      end
    if exists(select i.name from deleted d ,inserted i where d.id=i.id and i.name<>d.name)
      begin
        select @n=i.name,@o=d.name from deleted d ,inserted i where d.id=i.id and i.name<>d.name --更新列
        select @o='Exam.'+@o
        exec sp_rename @o,@n
      end
  end

--------------------------------(以下为处理函数生成脚本,利用在以上处理代码中)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_F]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_F]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_h]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_h]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE FUNCTION f_F(
@s   varchar(8000),   --待分拆的字符串
@split varchar(10)     --数据分隔符
)RETURNS @re TABLE(col varchar(100))
AS
BEGIN
  DECLARE @splitlen int
  SET @splitlen=LEN(@split+'a')-2
  WHILE CHARINDEX(@split,@s)>0
  BEGIN
     INSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1))
     SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')
  END
  INSERT @re VALUES(@s)
  RETURN
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE FUNCTION f_h(
@s   varchar(8000),   --待分行的字符串
@split varchar(10)     --数据分隔符
)RETURNS @re TABLE(col int)
AS
BEGIN
  insert  @re
  select * from f_f(@s,@split)
  update @re set col=8 where col=7
  update @re set col=col+1 where col<>8
  update @re set col=1 where col=8
  RETURN
END

 

 

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

----------------(以下为需要增加的表说明)

  班次表,表名bc,利用name字段值生成Exam表的字段。
id name Start End
1 早读 07:15:00 07:35:00
2 升旗 07:40:00 08:00:00
3 晚督修 18:10:00 22:20:00
       

 
custid
outid
教师
早读
升旗
晚督修
7
00010006
 
1
3
11
00010010
 
1
5
30
00010203
1,3,5
1
1
55
00010301
 
1
2
71
00020104
 
1
3
75
00020108
 
1
1,5
80
00020205
1,3,5
1
1,5
84
00020209
1,3,5
1
1,5
90
00020215
1,3,5
1
4,7
94
00020219
1,3,5
1
5,7
103
00020303
 
1
1,3
112
00020312
 
1
4,7
113
00020313
 
1
4
114
00020314
 
1
5,7
115
00020315
 
1
5,7
126
00020402
2,4,6
1
1,4
129
00020405
2,4,6
1
2,4
134
00020410
2,4,6
1
2,3
138
00020414
2,4,6
1
3,7
139
00020415
2,4,6
1
3,7
149
00020501
 
1
1,5
151
00020503
 
1
2
154
00020506
 
1
1,5
157
00020509
 
1
5,7
166
00020603
 
1
3,5
181
00020705
 
1
4,7
184
00020708
 
1
2,7
187
00020803
 
1
1
190
00020806
 
1
2,3
196
00020906
 
1
3
4011
00020109
 
1
3,7
4012
00020325
 
1
2,4
4013
00020425
2,4,6
1
4
4014
00020910
 
1
2
说明:从外部客户排班文件导入后形成的系统所需的数据格式与结构,表名Exam, 注意,除了Custid,Outid,教师三列外,其余的字段由bc表的name字段值生成,数据类型统一为Varchar(255)。其中的单位数字表示星期几,如早读字段值中的'1,3,5'表示星期一、三、五,其它的依此类推。

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • .Net研究栏目导航
    网站频道导航