plsql - For Loop in Oracle / Toad -
i new oracle , pl/sql , trying following.
i returning column names table name stored in variable
variable v_table varchar2(100) begin select 'mytable' :v_table dual; end; select column_name all_tab_columns table_name = :v_table
this returns rowset
column_name colname1 colname2 colname3
i loop through returned rowset , stats each column.
select count distinct(colname1), min(colname1), max(colname1) :v_table group min(colname1), max(colname1)
however cannot figure out how loop through rowset.
by using below can display column names of table , append sql query per requirement...
declare v_table varchar2(100):='table_name'; type nt_var1 table of varchar2(30); typ_nt nt_var1; begin select column_name bulk collect typ_nt all_tab_columns table_name =v_table order column_id; in 1..typ_nt.count loop dbms_output.put_line('column '||i||': '||typ_nt(i)); end loop; end;
note: should run select query dynamically means use of execute immediate why because passing table_name @ run time.
edit after editing query in loop below.
declare v_table varchar2(100):='table_name'; type nt_var1 table of varchar2(30); typ_nt nt_var1; var2 varchar2(2000); var3 varchar2(2000); begin select column_name bulk collect typ_nt all_tab_columns table_name =v_table order column_id; in 1..typ_nt.count loop dbms_output.put_line('column '||i||': '||typ_nt(i)); execute immediate 'select to_char(min('||typ_nt(i)||')),to_char(max('||typ_nt(i)||')) '||v_table var2,var3; dbms_output.put_line(var2||' '||var3); end loop; end;
note: find max data length of column present in table , assign same data type var2 , var3 variables.
Comments
Post a Comment