Matt Stone
Matt Stone

Reputation: 41

Modify oracle sql query output format

I want to change output format of my oracle sql script. Consider I have a script named: active_user.sh which inside it I just write my query. Now the problem is when I bash the script the output displayed without caption and only values are shown.

The script is:

export CONNECT_STRING=$1
if [ x$2 == x ]
then echo First Parameter is connection string to DB and Second parameter have to be ORACLE_HOME variable && exit 1
else export ORACLE_HOME=$2
fi
export ORACLE_SID=OMEGA #fake
export PATH=$ORACLE_HOME/bin:$PATH
RAND=$$
sqlplus -s /nolog <<-EOF > /tmp/${RAND}.sql_out.temp

connect $CONNECT_STRING
set HEADING OFF
set PAGESIZE 0
set linesize 120
col metric_name format a40
col value format 999999990.9999
select count(*) from v\$session where username is not null  and status='ACTIVE';
EOF

cat /tmp/${RAND}.sql_out.temp

And this is the command to run the script and the output is:

[root@oracle-test scripts]# ./active_users.sh "ora/orapass123@mydb" /opt/oracle/instantclient_11_2
         1

    23.0000

But when I run the query in sqlplus it returns something like this:

COUNT(*)
----------
1

Upvotes: 2

Views: 5180

Answers (1)

Aitor
Aitor

Reputation: 3429

If you want to print the column headings, you have to put set HEADING ON.

You can also using SPOOL command, something like this (sorry, I can't testint now):

spool /tmp/${RAND}.sql_out.temp
set heading on
set pagesize 1000
set tab on
set linesize 120 wrap off
column "yourcount" format a40
col metric_name format a40
col value format 999999990.9999
select count(*) yourcount from v\$session where username is not null  and status='ACTIVE';

Upvotes: 5

Related Questions