看起来您不需要使用动态SQL,因此可以静态声明一个游标,然后使用该游标的
%rowtype
要创建集合类型:
declare
cursor c_my_cursor is select col1, col2, col3 from table_test;
type t_my_table is table of c_my_cursor%rowtype;
v_ret t_my_table;
begin
open c_my_cursor;
fetch c_my_cursor bulk collect into v_ret;
close c_my_cursor;
--DBMS_OUTPUT.PUT_LINE('v_ret = '||v_ret.count);
for i in v_ret.first..v_ret.last loop
DBMS_OUTPUT.PUT_LINE('col1: '||v_ret (i).col1||', col2: '||v_ret (i).col2||', Col3: '||v_ret (i).Col3);
end loop;
end;
/
这不会循环光标,只会打开它,对集合执行单个批量提取,然后再次关闭它。
如果出于某种原因选择了同一列两次,或者只想坚持X/Y/Z命名(或您想要的任何方案),则可以在光标查询中对列进行别名:
cursor c_my_cursor is select col1 as colx, col2 as coly, col3 as colz from table_test;
...
DBMS_OUTPUT.PUT_LINE('colX: '||v_ret (i).colX||', colY: '||v_ret (i).colY||', ColZ: '||v_ret (i).ColZ);