--创建游标
declare DataPoint cursor
--读取部门表下级编号的数据
for select b_PriorID_i,b_Name_c,b_StaffOfDep from b_Hand where b_PriorID_i>@ID
--打开游标
open DataPoint
--读取值到
fetch next from DataPoint into @PriorID,@GetName,@HandName
while @@FETCH_STATUS=0
begin
--set @sql= N'select @BuMenNames=b_StaffOfDep,@PeopleNames=b_Name_c from b_Hand where b_PriorID_i='+CAST(@PriorID AS varchar(10))
--exec sp_executesql @sql,N'@BuMenNames varchar(50) out,@PeopleNames varchar(50) out',@HandName out,@GetName out
--将数据封装到临时表里面
insert into #HandInfo values(@PriorID,@GetName,@HandName)
--读取下一行值
fetch next from DataPoint into @PriorID,@GetName,@HandName
end
--关闭游标
close DataPoint
--释放对象
deallocate DataPoint
select * from #HandInfo
declare DataPoint cursor
--读取部门表下级编号的数据
for select b_PriorID_i,b_Name_c,b_StaffOfDep from b_Hand where b_PriorID_i>@ID
--打开游标
open DataPoint
--读取值到
fetch next from DataPoint into @PriorID,@GetName,@HandName
while @@FETCH_STATUS=0
begin
--set @sql= N'select @BuMenNames=b_StaffOfDep,@PeopleNames=b_Name_c from b_Hand where b_PriorID_i='+CAST(@PriorID AS varchar(10))
--exec sp_executesql @sql,N'@BuMenNames varchar(50) out,@PeopleNames varchar(50) out',@HandName out,@GetName out
--将数据封装到临时表里面
insert into #HandInfo values(@PriorID,@GetName,@HandName)
--读取下一行值
fetch next from DataPoint into @PriorID,@GetName,@HandName
end
--关闭游标
close DataPoint
--释放对象
deallocate DataPoint
select * from #HandInfo
本文详细介绍了使用SQL游标技术从部门表中查询并封装数据的过程,包括创建游标、读取数据、封装到临时表及关闭游标的步骤,展示了其在数据操作中的高效性和灵活性。
7230

被折叠的 条评论
为什么被折叠?



