Hide SQL query in spool file

This is a common problem I have: I need to generate a SQL script to be called from another SQL script in SQLplus. Best example: export user names, using this command for example:
select 'alter user ' || username || ' identified by values ''' || password || ''';' from dba_users;

To avoid the usually useful output in this case I used to set a few options, such as in:

set heading off pages 0 trimspool on lines 120 feedback off echo off termout off

Inside a shell script this could look as follows:

$ORACLE_HOME/bin/sqlplus / as sysdba <<EOF
 set heading off pages 0 trimspool on lines 120 feedback off echo off termout off
 spool /tmp/users.sql
 select 'alter user ' || name || ' identified by values ''' || password || ''';'
 from dba_users;
 exit
 EOF
However, the resulting file users.sql contains the SQL command! I didn’t find a “set” option in SQLPlus to get rid of it so I ended up “grep”ping for “^alter” to only have the SQL commands. Then I found out that I could as well use sqlplus -S (capital S) to achieve the same. Very handy!
Advertisement

8 thoughts on “Hide SQL query in spool file

  1. Pingback: Blogroll Report 08/01/2009 – 15/01/2010 « Coskan’s Approach to Oracle

  2. Madhu

    Thanks Martin , i was scratching my head to achieve this , I too tried with trimspool on.But it didn’t work.Thanks once again

  3. Madhu

    Better try to create a sql file with all your commands. Eg. cmd.sql and then try running it along with your login. I do see some strange thing with a .sh file.
    sqlplus ‘/as sysdba’ @cmd.sql

      1. Rene Schrama

        Yes it is, because it will remove the SQL command. Interactive commands are always echoed to screen AND spool file.

Comments are closed.