【问题标题】:suppressing messages while running sql queries in a script在脚本中运行 sql 查询时抑制消息
【发布时间】:2011-05-21 19:57:50
【问题描述】:

我正在尝试编写一个简单的查询脚本来获取表中的行数。但是,我面临抑制各种 oracle 消息的问题。我感兴趣的是输出:

这是我的脚本:

#!/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

我的输出如下所示:

.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 禁止所有这些消息吗?

【问题讨论】:

    标签: sql oracle shell scripting sqlplus


    【解决方案1】:

    您必须将大写的S 选项添加到sqlplus

    帮助消息(Oracle 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/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
    

    您只能从 DBMS_OUTPUT 缓冲区和 select 语句中获得输出。

    【讨论】:

      【解决方案2】:

      静默模式需要使用sqlplus -s

      #!/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
      

      【讨论】:

      • 我同意通过添加 -s 没有任何消息,即使是我想看到的消息。问题是如何隐藏所有这些消息并仅显示我想看到的消息,例如,如果我添加 dbms_output.put_line('Hello Word') 我只想看到 Hello Word。我该怎么做???
      • @vogash,详情见我的回答
      【解决方案3】:

      试试-s 标志。例如,

      sqlplus /s /nolog <<EOF
      

      ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-18
        • 1970-01-01
        • 1970-01-01
        • 2017-01-10
        • 2014-10-17
        • 1970-01-01
        相关资源
        最近更新 更多