create procedure [dbo].[getallstuclicktime] @stime nvarchar(20),--开始时间 @etime nvarchar(20)--结束时间 --时间为第一个循环体 以这个时间段为范围 as begin declare @days int declare @datediff int declare @datetime datetime declare @count int
create procedure [dbo].[getallstuclicktime]
@stime nvarchar(20),--开始时间
@etime nvarchar(20)--结束时间
--时间为第一个循环体 以这个时间段为范围
as
begin
declare @days int
declare @datediff int
declare @datetime datetime
declare @count int
declare @classid nvarchar(50)
declare @people nvarchar(20)--学生卡号
declare @type nvarchar(50)--打卡类型
declare @typevalue nvarchar(50)--打卡类型value值
set datefirst 1
set @count = 0
set @days = 0
set @datediff = datediff(day,@stime,@etime)
while @days begin
set @datetime = dateadd(day,@days,@stime)--第二个循环取出学生表中的学生姓名,班级编号
--新建游标
declare stuclicktime cursor for select classname,ttcard from dbo.vw_studentinfo
--打开游标
open stuclicktime
--从游标里取出数据给 变量 赋值
fetch next from stuclicktime into @classid,@people
begin
--判断游标的状态
-- 0 fetch语句成功
-- -1 fetch语句失败或此行不在结果集中
-- -2被提取的行不存在
while @@fetch_status = 0
begin
--第三个循环体,将每个学生的每个类型循环
--新建游标
declare stuclicktimetyp cursor for select pbc_name,pbc_value from tb_code where pbc_node=48
--打开游标
open stuclicktimetyp
--从游标里取出数据给 变量 赋值
fetch next from stuclicktimetyp into @type,@typevalue
begin
--判断游标的状态
-- 0 fetch语句成功
-- -1 fetch语句失败或此行不在结果集中
-- -2被提取的行不存在
while @@fetch_status = 0
begin--判断新表中是否存在此条循环出来的数据
set @count = (select isnull(count(*),0) from dbo.sm_checkinout where userid=@people and resulttype=@type and clocktime=@datetime )
if(@count = 0)
begin
insert into sm_checkinout(classid,userid,resulttype,clocktime,resulttypevalue) values(@classid,@people,@type,@datetime,@typevalue)
end--用游标去取下一条记录
fetch next from stuclicktimetyp into @type,@typevalue
end
end
--关闭游标
close stuclicktimetyp
--撤销游标
deallocate stuclicktimetyp
--用游标去取下一条记录
fetch next from stuclicktime into @classid,@people
end
end
--关闭游标
close stuclicktime
--撤销游标
deallocate stuclicktime
set @days = @days + 1 --循环完了一天然后加一
end
end