2010-12-07 59 views
6

我正在尝试编写一个简单的查询脚本,该脚本可以获取表中行的cnt数。不过,我面临的问题是压制各种oracle消息。所有我感兴趣的是输出:在脚本中运行sql查询时抑制邮件

这里是我的脚本:

#!/usr/bin/ksh 
sqlplus /nolog <<EOF 
connect user/[email protected] 
set serveroutput on 
set heading off 
set feedback off 
select count(*) from table; 
exit; 
EOF 

我的输出是这样的:

.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 

我要的是数70没有任何的消息,让我可以定期写入日志等。我知道我可以解析这个数字,但每当我的查询或模式发生变化时,我都必须改变它。我不能只是要求mysqlplus压制所有这些消息吗?

回答

7

您需要使用的sqlplus -s静默模式

#!/usr/bin/ksh 
sqlplus -s /nolog <<EOF 
connect user/[email protected] 
set serveroutput on 
set heading off 
set feedback off 
select count(*) from table; 
exit; 
EOF 
+0

我同意通过添加-s,即使是我想看到的那些消息也没有任何消息。问题是如何隐藏所有这些消息,并只显示我想看到的消息,例如,如果我添加 dbms_output.put_line('Hello Word') 我想只看到Hello Word。我怎样才能做到这一点??? – vogash 2013-12-25 10:42:22

4

尝试-s标志。例如,

sqlplus /s /nolog <<EOF 

...

6

你必须大写S选项添加到sqlplus

(附带甲骨文11.2.0.4.0的SQLPLUS)的帮助信息规定:

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

的东西,如

$ sqlplus -S /nolog << EOF 
connect user/[email protected] 
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 

你只能得到从输出DBMS_OUTPUT缓冲区和select语句的结果。