oracle存储过程游标问题,多层循环游标,插入中间表

[复制链接]
查看11 | 回复3 | 2011-6-17 06:31:13 | 显示全部楼层 |阅读模式
forsin1in(
查询语句
)
LOOP
forsin2in(
查询语句
)LOOP
forsin3in(
查询语句
)LOOP
插入语句
ENDLOOP;
ENDLOOP;
ENDLOOP;
sin1游标里查询出的结果,会做为sin2、sin3的查询条件。最后把游标sin1、san2、san3的数据插入到一张中间表中,该怎么插入?
回复

使用道具 举报

千问 | 2011-6-17 06:31:13 | 显示全部楼层
以hr用户下的employees、departments、locations这三张表为列,sin1得到的是雇员的全名和对应的部门id,并将该部门的id作为sin2查询时的条件,sin2得到的是该部门id所对应的部门名和对应的位置id,并将该位置id作为sin3查询使得条件,最后sin3得到的就是该位置id所应得城市,并且在sin3这个循环里将sin1里雇员的全名,sin2里的部门名以及sin3里的city作为一条记录插入到sin_insert表里.
附上代码:
first:
createtablesin_insert(full_namevarchar2(50),department_namevarchar2(30),city
varchar2(30));

then:
createorreplaceproceduretestloop
as
begin
forsin1in(selectfirst_name||last_namefull_name,department_idfrom
employees)loop
forsin2in(selectdepartment_name,location_idfromdepartmentswhere
department_id=sin1.department_id)loop
forsin3in(selectcityfromlocationswhere
location_id=sin2.location_id)loop
insertintosin_insertvalues
(sin1.full_name,sin2.department_name,sin3.city);
endloop;
endloop;
endloop;
end;









<h4class=\"ask\">追问


能运行,没有插入数据!方便吗?能加QQ说嘛?
回复

使用道具 举报

千问 | 2011-6-17 06:31:13 | 显示全部楼层
你试一下以下的查询语句看能否查到数据:
selecte.first_name||e.last_namefull_name,d.department_name,l.city
fromhr.employeesejoinhr.departmentsdon
(e.department_id=d.department_id)joinhr.locationslon
(d.location_id=l.location_id);
按道理来说如果能查到数据的话,那么在执行完上面的执行过程之后就应该有数据插入到sin_insert才对。
回复

使用道具 举报

千问 | 2011-6-17 06:31:13 | 显示全部楼层
我游标里的数据集,是有数据的!我的QQ88628004,朋友能帮我解决一下吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行