Jika Anda benar perlu select * from
nama tabel dinamis , maka saya mungkin akan menggunakan dbms_sql
Ketik untuk rekaman :
create type tq84_varchar2_tab as table of varchar2(4000);
/
Ketik untuk kumpulan hasil (yang merupakan larik catatan ):
create type tq84_varchar2_tab_tab as table of tq84_varchar2_tab;
/
Fungsi yang melakukan pemilihan dan mengembalikan instance dari set hasil :
create or replace function tq84_select_star_from_table(table_name in varchar2)
return tq84_varchar2_tab_tab
as
stmt_txt varchar2(4000);
cur number;
columns_desc dbms_sql.desc_tab;
column_cnt number;
result_set tq84_varchar2_tab_tab;
begin
stmt_txt := 'select * from ' || table_name;
cur := dbms_sql.open_cursor;
dbms_sql.parse(cur, stmt_txt, dbms_sql.native);
dbms_sql.describe_columns(cur, column_cnt, columns_desc);
dbms_sql.close_cursor(cur);
stmt_txt := 'select tq84_varchar2_tab(';
for i in 1 .. column_cnt loop
if i != 1 then
stmt_txt := stmt_txt || ',';
end if;
stmt_txt := stmt_txt || columns_desc(i).col_name;
end loop;
stmt_txt := stmt_txt || ') from ' || table_name;
-- dbms_output.put_line(stmt_txt);
execute immediate stmt_txt
bulk collect into result_set;
return result_set;
end tq84_select_star_from_table;
Fungsi tersebut kemudian dapat digunakan dengan sesuatu seperti:
declare
records tq84_varchar2_tab_tab;
begin
records := tq84_select_star_from_table('user_objects');
for i in 1 .. records.count loop
dbms_output.put_line (records(i)(5) || ': ' || records(i)(1));
end loop;
end;
/