plsql - Oracle cusor return result and get value by dynamic column name -
as tittle, want value column name paramater in cursor result.
i not find answer after searching long time,could give help?
--common usage begin rowvalue in (select code, name tablea) loop -- rowvalue.code rowvalue.name end loop; end begin rowvalue in (select * tablea) loop -- tableb keep column's names talbea columnnames in (select name tableb) loop -- want use like: rowvalue.(columnnames.name) end loop; end loop; end
you can use dbms_sql, this:
declare l_cursor integer; l_column_count integer; l_column_descriptions sys.dbms_sql.desc_tab; l_column_value varchar2 (4000); l_status integer; type col_map_tab_type table of integer index varchar2 (30); col_map_tab col_map_tab_type; begin l_cursor := sys.dbms_sql.open_cursor; -- parse sql sys.dbms_sql.parse (c => l_cursor, statement => 'select * dba_objects rownum <= 10', language_flag => sys.dbms_sql.native); -- describe columns sys.dbms_sql.describe_columns (c => l_cursor, col_cnt => l_column_count, desc_t => l_column_descriptions); in 1 .. l_column_count loop col_map_tab (l_column_descriptions (i).col_name) := i; dbms_sql.define_column (l_cursor, i, l_column_value, 4000); end loop; l_status := sys.dbms_sql.execute (l_cursor); while (sys.dbms_sql.fetch_rows (l_cursor) > 0) loop column_names in (select column_name dba_tab_columns table_name = 'dba_objects' , column_name 'o%') loop dbms_sql.column_value (l_cursor, col_map_tab (column_names.column_name), l_column_value); dbms_output.put_line (column_names.column_name || ' = ' || l_column_value); end loop; end loop; sys.dbms_sql.close_cursor (l_cursor); end;
Comments
Post a Comment