How to get the dynamic select results of EXECUTE within pl/sql from Oracle sqlplus?
I'm writing a simple sqlplus script to collect the sum of all NUMBER columns of a given table --
SET SERVEROUTPUT ON
DECLARE
CURSOR column_cur IS
SELECT column_name FROM ALL_TAB_COLS
WHERE owner = '&scheme_name' AND table_name = '&table_name'
AND data_type = 'NUMBER';
sql_query VARCHAR2(32767);
BEGIN
sql_query := 'select ';
FOR column_rec IN column_cur LOOP
sql_query := sql_query || 'SUM(' || column_rec.column_name ||
') "SUM(' || column_rec.column_name || ')", ';
END LOOP;
sql_query := substr(sql_query, 0, length(sql_query)-2) || -- remove trailing ', '
' from &scheme_name' || '.&table_name';
EXECUTE IMMEDIATE sql_query;
END;
/
The dynamically generated SQL statement, when executed, gives something like --
SUM(X) SUM(Y) SUM(Z)
______ ______ ______
111 222 333
However, even with SET SERVEROUTPUT ON, running the sqlplus script gives only --
PL/SQL procedure successfully completed.
Thanks.
Jerry