我的程序中有一个如下所示的示例查询:
result_rec mypkg.mytype;
OPEN CUR1 FOR
select col1, col2, col3 from table1 where something = 'a'; --rows will always be 50
LOOP
FETCH CUR1
INTO myrectype;
EXIT WHEN CUR1%NOTFOUND;
result_rec.col1 := myrectype.col1;
result_rec.col2 := myrectype.col2;
result_rec.col3 := myrectype.col3;
PIPE ROW (result_rec);
END LOOP;
正如您所看到的,每次循环50次。有一个更好的方法吗?像BULK COLLECT INTO这样的东西?我该如何实现呢?
答案 0 :(得分:6)
在Oracle 10g(可能是9i)中,Oracle将自动批量收集隐式游标。所以代码如下:
DECLARE
result_rec mypkg.mytype;
BEGIN
for i in (select col1, co2, col3 from table1 where something = 'a')
loop
result_rec.col1 := i.col1;
result_rec.col2 := i.col2;
result_rec.col3 := i.col3;
pipe_row (result_rec);
end loop;
END;
只会将上下文从PL / SQL引擎切换到SQL引擎,以便每100行获取一次记录。在SQL跟踪(dbms_monitor.session_trace_enable()
)下运行它,看看!
答案 1 :(得分:3)
您可以尝试以下操作。
DECLARE
type tab_result_rec IS TABLE OF mypkg.mytype INDEX BY PLS_INTEGER;
t_result_rec tab_result_rec;
BEGIN
select col1, col2, col3 bulk collect into t_result_rec
from table1 where something = 'a'; --rows will always be 50
--
for i in 1..t_result_rec.count LOOP
PIPE ROW (t_result_rec(i));
end loop;
END;