oracle存储过程游标问题,多层循环游标,插入中间表
forsin1in(查询语句)LOOPforsin2in(查询语句)LOOPforsin3in(查询语句)LOOP插入语句ENDLOOP;ENDLOOP;ENDLOOP;...
for sin1 in(
查询 语句
)
LOOP
for sin2 in(
查询 语句
)LOOP
for sin3 in(
查询 语句
)LOOP
插入语句
END LOOP;
END LOOP;
END LOOP;
sin1游标里查询出的结果,会做为sin2、sin3的查询条件。最后把游标sin1、san2、san3的数据插入到一张中间表中,该怎么插入? 展开
查询 语句
)
LOOP
for sin2 in(
查询 语句
)LOOP
for sin3 in(
查询 语句
)LOOP
插入语句
END LOOP;
END LOOP;
END LOOP;
sin1游标里查询出的结果,会做为sin2、sin3的查询条件。最后把游标sin1、san2、san3的数据插入到一张中间表中,该怎么插入? 展开
展开全部
以hr用户下的employees、departments、locations这三张表为列,sin1得到的是雇员的全名和对应的部门id,并将该部门的id作为sin2查询时的条件,sin2得到的是该部门id所对应的部门名和对应的位置id,并将该位置id作为sin3查询使得条件,最后sin3得到的就是该位置id所应得城市,并且在sin3这个循环里将sin1里雇员的全名,sin2里的部门名以及sin3里的city作为一条记录插入到sin_insert表里.
附上代码:
first:
create table sin_insert(full_name varchar2(50),department_name varchar2(30),city
varchar2(30));
then:
create or replace procedure testloop
as
begin
for sin1 in (select first_name||last_name full_name,department_id from
employees) loop
for sin2 in (select department_name,location_id from departments where
department_id=sin1.department_id) loop
for sin3 in (select city from locations where
location_id=sin2.location_id) loop
insert into sin_insert values
(sin1.full_name,sin2.department_name,sin3.city);
end loop;
end loop;
end loop;
end;
附上代码:
first:
create table sin_insert(full_name varchar2(50),department_name varchar2(30),city
varchar2(30));
then:
create or replace procedure testloop
as
begin
for sin1 in (select first_name||last_name full_name,department_id from
employees) loop
for sin2 in (select department_name,location_id from departments where
department_id=sin1.department_id) loop
for sin3 in (select city from locations where
location_id=sin2.location_id) loop
insert into sin_insert values
(sin1.full_name,sin2.department_name,sin3.city);
end loop;
end loop;
end loop;
end;
更多追问追答
追问
能运行,没有插入数据!方便吗?能加QQ说嘛?
追答
你试一下以下的查询语句看能否查到数据:
select e.first_name||e.last_name full_name,d.department_name,l.city
from hr.employees e join hr.departments d on
(e.department_id=d.department_id) join hr.locations l on
(d.location_id=l.location_id);
按道理来说如果能查到数据的话,那么在执行完上面的执行过程之后就应该有数据插入到sin_insert才对。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询