I am junior oracle dba and have a question about PL/SQL. May you give me some advice please? I would like to know that is it possible to put SELECT query into BEGIN ... END with SPOOL. The purpose of this code is using array content to be the source of SELECT FROM. Thank you very much for your kind help. Here is my shell script with PL/SQL code and it's not working:
#! /bin/bash
$ORACLE_HOME/bin/sqlplus "sysmonitor/********" << EOF
declare
type array_t is table of varchar2(30);
array array_t := array_t(
'DBUSER_UNEW_TABLE1',
'DBUSER_UNEW_TABLE2',
'DBUSER_UNEW_TABLE3'
);
begin
for i in 1..array.count loop
spool test.log
select * from array(i);
spool off;
end loop;
end;
/
EOF
After reading the suggestion from the nice people as below, I made some change as below. However, I still don't know how to spool each result in a single file after looping SELECT the tables (DBUSER_UNEW_TABLE1,DBUSER_UNEW_TABLE2,DBUSER_UNEW_TABLE3). May you give me some advice please?
$ORACLE_HOME/bin/sqlplus "sysmonitor/********" << EOF
declare
type array_t is table of varchar2(30);
array array_t := array_t(
'DBUSER_UNEW_TABLE1',
'DBUSER_UNEW_TABLE2',
'DBUSER_UNEW_TABLE3'
);
begin
for i IN 1 .. array.count
loop
select * from array(i);
end loop;
end;
spool test.log
select * from array(i);
spool off
exit
EOF
If I run the sql code under sqlplus command line, it returs error. select * from array(i); * ERROR at line 11: ORA-06550: line 11, column 20: PL/SQL: ORA-00933: SQL command not properly ended ORA-06550: line 11, column 1: PL/SQL: SQL Statement ignored
declare
type array_t is table of varchar2(30);
array array_t := array_t(
'DBUSER_UNEW_TABLE1',
'DBUSER_UNEW_TABLE2',
'DBUSER_UNEW_TABLE3'
);
begin
for i IN 1 .. array.count
loop
select * from array(i);
end loop;
end;
/