使用游标查询子部门下的数据
2012-07-26 14:47
232 查看
使用游标查询数据
--这个是根据部门ID,和公司,找到这个部门下的子部门,然后根据子部门ID查找该部门有多少人多少辆车
-- UP3_Tdept_SelectByTDtPID 2
alter proc UP3_Tdept_SelectByTDtPID
@TDtID int
as
begin
create
table #aa
(
TDtID int,
TDtName varchar(50),
car int,
people int
)
declare @car
int
DECLARE Employee_Cursor
CURSOR local FOR(select TDtID
from tdept where tdtpid=@TDtID
)--父部门下的子部门的部门ID(TDtID)
OPEN Employee_Cursor
FETCH NEXT
FROM Employee_Cursor into @car
WHILE
@@FETCH_STATUS = 0
BEGIN
insert
into #aa(TDtID,TDtName,car
,people)
select TDtID
,TDtName,dbo.Select_car_bydep2(TDtID)
,dbo.[Select_car_byperson2](TDtID)--将TDtID作为参数传给函数并将查询出来的数据插入临时表中
from tdept
where TDtID
=@car
order
by TDtID desc
FETCH NEXT
FROM Employee_Cursor into @car
END
select
* from #aa
CLOSE Employee_Cursor;
drop
table #aa
end
--这个是根据部门ID,和公司,找到这个部门下的子部门,然后根据子部门ID查找该部门有多少人多少辆车
-- UP3_Tdept_SelectByTDtPID 2
alter proc UP3_Tdept_SelectByTDtPID
@TDtID int
as
begin
create
table #aa
(
TDtID int,
TDtName varchar(50),
car int,
people int
)
declare @car
int
DECLARE Employee_Cursor
CURSOR local FOR(select TDtID
from tdept where tdtpid=@TDtID
)--父部门下的子部门的部门ID(TDtID)
OPEN Employee_Cursor
FETCH NEXT
FROM Employee_Cursor into @car
WHILE
@@FETCH_STATUS = 0
BEGIN
insert
into #aa(TDtID,TDtName,car
,people)
select TDtID
,TDtName,dbo.Select_car_bydep2(TDtID)
,dbo.[Select_car_byperson2](TDtID)--将TDtID作为参数传给函数并将查询出来的数据插入临时表中
from tdept
where TDtID
=@car
order
by TDtID desc
FETCH NEXT
FROM Employee_Cursor into @car
END
select
* from #aa
CLOSE Employee_Cursor;
drop
table #aa
end
相关文章推荐
- Oracle 使用函数 function查询数据返回游标
- 通过python使用游标查询Elasticsearch数据并写入文件
- sqlserver 数据横向查询和游标使用
- 使用游标批量获取数据提高查询性能
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
- Oracle使用游标查询所有数据表备注
- 使用Eclipse远程连接MySQL,以定时查询数据为示例
- 转jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用
- Oracle中使用游标获取指定数据表的所有字段名对应的字符串
- CYQ.Data 轻量数据层之路 使用篇二曲 MAction 数据查询(十三)
- Medoo Get的使用:查询单条数据
- 在存储过程中使用游标循环表,组织数据
- 使用SQL 查询 第一条数据
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- 查询数据库数据使用HashMap保存
- MySQL查询中使用Concat关键字来拼接中文字符乱码(不同的数据类型拼接)解决方式
- 使用PDF.NET数据开发框架的实体操作语言OQL构造复杂查询条件
- Yii 1.1.17 三、数据库连接、定义模型、数据查询、验证登录、SESSION使用与URL生成
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果