Plsql PL/SQL:我得到了错误;遇到符号“;开放式;应为以下情况之一时:。(*@%&;&x2B;/”;
我的代码:Plsql PL/SQL:我得到了错误;遇到符号“;开放式;应为以下情况之一时:。(*@%&;&x2B;/”;,plsql,Plsql,我的代码: create table dep_emp_ast( cod_dep number(3), cod_ang number(3)); declare type cref is ref cursor; c cref; type tab_imb is table of dept_ast.department_id%type; t tab_imb:=tab_imb(); v_ang emp_ast.employee_id%type; begin select dist
create table dep_emp_ast(
cod_dep number(3),
cod_ang number(3));
declare
type cref is ref cursor;
c cref;
type tab_imb is table of dept_ast.department_id%type;
t tab_imb:=tab_imb();
v_ang emp_ast.employee_id%type;
begin
select distinct department_id
bulk collect into t
from dept_ast;
forall i in 1..t.count
open c for select employee_id
from emp_ast
where department_id=t(i);
loop
fetch c into v_ang
insert into dep_emp_ast
values(t(i),v_ang);
exit when c%notfound;
end loop;
close c;
end;
/
我的错误是无法在那里打开光标。但是为什么?我想重新打开光标,并对t(I)的每个值重新使用它。后面必须跟一个DML语句:
例如:
forall i in depts.first..depts.last
delete employees_temp
where department_id = depts(i);
我想你想要的是:
declare
c sys_refcursor;
type tab_imb is table of dept_ast.department_id%type;
t tab_imb:=tab_imb();
v_ang emp_ast.employee_id%type;
begin
select distinct department_id
bulk collect into t
from dept_ast;
for i in 1..t.count loop
open c for
select employee_id
from emp_ast
where department_id=t(i);
loop
fetch c into v_ang;
insert into dep_emp_ast
values(t(i),v_ang);
exit when c%notfound;
end loop;
close c;
end loop;
end;
可以简化为
begin
for d in (
select distinct department_id
from dept_ast
)
loop
for e in (
select employee_id
from emp_ast
where department_id = d.department_id
)
loop
insert into dep_emp_ast
values (d.department_id, e.employee_id);
end loop;
end loop;
end;
这可以归结为
insert into dep_emp_ast (cod_dep, cod_ang)
select e.department_id, e.employee_id
from emp_ast e
where e.department_id in
( select department_id
from dept_ast );