我正在使用Oracle 11G。可以说我将数据加载到的集合中ROWTYPE
。
emp_tab
如果id
不使用任何LOOP就作为参数传递,如何从集合中提取名称或年龄?
create table emp (id number, name varchar2(20), age number);
insert into emp values(10,'Tom',20);
insert into emp values(20,'Nicole',30);
commit;
select * from emp;
declare
TYPE emp_t IS TABLE OF emp%ROWTYPE INDEX BY PLS_INTEGER;
emp_tab emp_t;
begin
select id,name,age bulk collect into emp_tab from emp;
for idx in 1..emp_tab.count loop
dbms_output.put_line(emp_tab(idx).name);
end loop;
end;
您需要使用来对集合进行索引emp.id
,并填充无法使用的集合bulk collect
(至少不是在11g中-在20c中可能是)。
declare
type emp_t is table of emp%rowtype index by pls_integer;
emp_tab emp_t;
idx emp.id%type;
begin
for r in (
select id, name, age
from emp
)
loop
emp_tab(r.id).name := r.name;
emp_tab(r.id).age := r.age;
end loop;
idx := emp_tab.first;
while idx is not null loop
dbms_output.put_line(emp_tab(idx).name);
idx := emp_tab.next(idx);
end loop;
end;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句