Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Issue with spool file name in SQL*Plus

Y.RamletAug 12 2022

RDBMS version: 19c 
Shell : Bash sell in Oracle Linux 7.9

I have a bash shell script called test.sh invokes sqlplus inside heredoc (shown below).
test.sh executes sample.sql file as shown below

I am passing a shell variable called $VAR2 to the sample.sql script

Everything seems to work except the spooled file name for the spool command inside sample.sql.

I am expecting a spool file named script_for_SALES.sql to be generated.

Instead, I get filenames like script_for_SALESsql.lst ! Any workaround for this ?
I tried few other variants also (mentioned at the bottom)

$ cat test.sh
#!/bin/bash

export VAR2=SALES

sqlplus / as sysdba <<-EOF
  @/path/in/linux/sample.sql $VAR2
  exit
EOF
$ cat /path/in/linux/sample.sql
set head off
set verify off
set echo off
set lines 140
set trim on
set pages 0
set feedback off

spool "/path/in/linux/script_for_&1.sql"
SELECT 'alter session set container = &1 ;' FROM DUAL;
select 'select sysdate from dual;' from dual;
prompt exit
spool off

exit

Executing test.sh

$ ./test.sh
<banner snipped for readability>
SQL> alter session set container = SALES ;
select sysdate from dual;
exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0


$
$ ls -l
total 12
-rw-r--r--. 1 oracle oinstall 287 Aug 12 16:52 sample.sql
-rw-r--r--. 1 oracle oinstall 69 Aug 12 16:56 script_for_SALESsql.lst
-rwxr-xr-x. 1 oracle oinstall 129 Aug 12 16:56 test.sh

If I try 

spool /path/in/linux/script_for_'&1'.sql 
or 
spool "/path/in/linux/script_for_'&1'.sql"

I get a spool file named script_for_'SALES'.sql. But, I don't want single quotes around SALES 

If I try spool /path/in/linux/script_for_&1.sql
Again, I get the spool file generated with the name script_for_SALESsql.lst
Any way to get the spool file with the name script_for_SALES.sql ?

This post has been answered by Frank Kulash on Aug 12 2022
Jump to Answer
Comments
Post Details
Added on Aug 12 2022
4 comments
1,458 views