利用存储过程把两个表的数据插入到一个新表中(存储过程中写两个游标)
2013-12-11 16:32
676 查看
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 <= @datediff --为天数(第几天第几天)
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
@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 <= @datediff --为天数(第几天第几天)
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
相关文章推荐
- 利用存储过程把两个表的数据插入到一个新表中(存储过程中写两个游标)
- 一个存储过程插入两个表数据,获得最近标识值
- 自己写的存储过程要记住一:(一个插入语句游标遍历数据替换关键字的过程)
- .若A,B两个实体是一对多的包含关系。利用存储过程,实现数据访问层层的一个实例。
- MYSQL存储过程:批量更新、插入数据、游标
- mysql利用存储过程插入测试数据
- 利用jdbc对数据表进行分页,操作存储过程(有参与无参,返回游标),函数等
- mysql利用存储过程批量插入数据
- Mysql利用存储过程插入400W条数据
- 一个循环插入数据到数据库的存储过程
- 自动生成对一个数据表的插入和更新的存储过程
- 如何利用存储过程插入更新IMAGE数据类型
- 利用存储过程读指定文件夹里所有txt文件,将数据插入数据库中
- 一个游标处理有两个结果集的存储过程
- MySql 存储过程使用游标循环插入数据示例
- MySql 存储过程使用游标循环插入数据示例
- mysql利用存储过程批量插入数据
- 两个存储过程:一、使用存储过程动态创建数据表。二、插入新记录。
- mysql利用存储过程批量插入数据
- mysql存储过程传入两个参数,循环插入数据