为多列构建 TYPE
Build TYPE for many columns
我有以下类型:
TYPE t_my_list is record(col1 VARCHAR2(4000),col2 varchar2(4000),col3 varchar2(4000));
Type listOfString is table of t_my_list;
然后我在做 BULK COLLECT:
EXECUTE IMMEDIATE v_stmt BULK COLLECT INTO v_ret;
如果我有 20 列怎么办?这样做我认为没有意义:
TYPE t_my_list is record(col1 VARCHAR2(4000),col2 varchar2(4000),....col20 varchar2(200);
还能如何构建 TYPE?
更新:
declare
TYPE t_my_list is record(colX VARCHAR2(4000),colY varchar2(4000),ColZ varchar2(4000));
Type listOfString is table of t_my_list;
v_stmt VARCHAR2(32000) := 'SELECT col1, col2, col2 FROM table_TEST';
v_ret listOfString := listOfString ();
begin
EXECUTE IMMEDIATE v_stmt BULK COLLECT INTO v_ret;
--DBMS_OUTPUT.PUT_LINE('v_ret = '||v_ret.count);
for i in v_ret.first..v_ret.last loop
DBMS_OUTPUT.PUT_LINE('colX: '||v_ret (i).colX||', colY: '||v_ret (i).colY||', ColZ: '||v_ret (i).ColZ);
--will be done second EXECUTE IMMEDIATE taking as parameter ColX, ColY and ColZ
end loop;
end;
看起来你并不真的需要使用动态 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);
我有以下类型:
TYPE t_my_list is record(col1 VARCHAR2(4000),col2 varchar2(4000),col3 varchar2(4000));
Type listOfString is table of t_my_list;
然后我在做 BULK COLLECT:
EXECUTE IMMEDIATE v_stmt BULK COLLECT INTO v_ret;
如果我有 20 列怎么办?这样做我认为没有意义:
TYPE t_my_list is record(col1 VARCHAR2(4000),col2 varchar2(4000),....col20 varchar2(200);
还能如何构建 TYPE?
更新:
declare
TYPE t_my_list is record(colX VARCHAR2(4000),colY varchar2(4000),ColZ varchar2(4000));
Type listOfString is table of t_my_list;
v_stmt VARCHAR2(32000) := 'SELECT col1, col2, col2 FROM table_TEST';
v_ret listOfString := listOfString ();
begin
EXECUTE IMMEDIATE v_stmt BULK COLLECT INTO v_ret;
--DBMS_OUTPUT.PUT_LINE('v_ret = '||v_ret.count);
for i in v_ret.first..v_ret.last loop
DBMS_OUTPUT.PUT_LINE('colX: '||v_ret (i).colX||', colY: '||v_ret (i).colY||', ColZ: '||v_ret (i).ColZ);
--will be done second EXECUTE IMMEDIATE taking as parameter ColX, ColY and ColZ
end loop;
end;
看起来你并不真的需要使用动态 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);