Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppressing messages while running sql queries in a script

I am trying to write a simple query-script that gets me cnt of rows in a table. However I am facing problem to suppress all sorts of oracle messages. All I am interested is the output:

Here is my script:

#!/usr/bin/ksh
sqlplus /nolog <<EOF
connect user/pswd@databse
set serveroutput on
set heading off
set feedback off
select count(*) from table;
exit;
EOF

My output looks like this:

.desktop% sh sql.ksh 
SQL*Plus: Release 10.2.0.2.0 - Production on Tue Dec 7 12:00:42 2010
Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.
SQL> Connected.
SQL> SQL> SQL> SQL> 
        70
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

All I want is the number 70 without any message so that I can write it to logs etc regularly. I know I can parse for the number but I ll have to change that every time my query or schema changes. Can't I just ask mysqlplus to suppress all those messages?

like image 255
codeObserver Avatar asked Dec 07 '10 20:12

codeObserver


3 Answers

You have to add the uppercase S option to sqlplus.

The help message (of sqlplus that comes with Oracle 11.2.0.4.0) specifies:

-S    Sets silent mode which suppresses the display of
      the SQL*Plus banner, prompts, and echoing of
      commands.

With something like

$ sqlplus -S /nolog << EOF
connect user/pswd@databse
set serveroutput on
set heading off
set feedback off
exec package.procedure($1); -- procedure that calls DBMS_OUTPUT procedures ...
select 2 from dual;
-- ...
exit;
EOF

you only get the output from the DBMS_OUTPUT buffer and the results from select statements.

like image 70
maxschlepzig Avatar answered Nov 15 '22 21:11

maxschlepzig


You need to use sqlplus -s for silent mode

#!/usr/bin/ksh
sqlplus -s /nolog <<EOF
connect user/pswd@databse
set serveroutput on
set heading off
set feedback off
select count(*) from table;
exit;
EOF
like image 39
josephj1989 Avatar answered Nov 15 '22 22:11

josephj1989


Try the -s flag. e.g.,

sqlplus /s /nolog <<EOF

...

like image 26
Emil Sit Avatar answered Nov 15 '22 22:11

Emil Sit