【问题标题】:Running Oracle script from bash从 bash 运行 Oracle 脚本
【发布时间】:2013-06-24 12:16:48
【问题描述】:

我的查询基本上是当我尝试从 bash 运行多个 oracle 脚本时,如何将 cmets 放在脚本之间。我尝试通过从双重中选择一个字符串来使用解决方法。但是输出格式不是很好。

谁能给我一个更好的方法。

我的代码

#!/bin/bash
#Run Script
    echo "-------------------------------"
    echo "***Running Script1***"
    echo "-------------------------------"
sqlplus -S UID/PSW@DB << EOF
whenever sqlerror exit sql.sqlcode;
set echo off 
set heading off
@/my/path/Script1
Select '--------------' from dual;
select '***Running Script1***' from dual;
Select '--------------' from dual;
@/my/path/Script2
exit;
EOF

输出

-------------------------------
***Running Script1***
-------------------------------
SP2-0310: unable to open file "my/path/Script1.sql"

--------------


***Running Script2***    

--------------

SP2-0310: unable to open file "my/path/Script2.sql"

预期输出

-------------------------------
***Running Script1***
-------------------------------
SP2-0310: unable to open file "my/path/Script1.sql"

--------------
***Running Script2***    
--------------    
SP2-0310: unable to open file "my/path/Script2.sql"

【问题讨论】:

    标签: oracle bash sqlplus


    【解决方案1】:

    尝试使用 SQL*Plus 的PROMPT 命令:

    $ cat tmp.sh
    #!/bin/bash
    
    sqlplus -S UID/PSW@DB << EOF
    whenever sqlerror exit sql.sqlcode
    set echo off
    set heading off
    
    prompt =======================
    prompt *** Running Script1 ***
    prompt =======================
    @/my/path/Script1
    
    prompt =======================
    prompt *** Running Script2 ***
    prompt =======================
    @/my/path/Script2
    
    exit
    EOF
    

    输出:

    $ ./tmp.sh
    =======================
    *** Running Script1 ***
    =======================
    SP2-0310: unable to open file "/my/path/Script1.sql"
    =======================
    *** Running Script2 ***
    =======================
    SP2-0310: unable to open file "/my/path/Script2.sql"
    

    【讨论】:

    • 太棒了。正是我想要的。
    【解决方案2】:

    怎么样

    Select '--------------' || chr(10) || '***Running Script1***' || chr(10) || '--------------' from dual;
    

    【讨论】:

      【解决方案3】:

      过去,我使用过dbms_output.put_line

      dbms_output.put_line('starting process at: '||to_char(sysdate,'HH24:MI:SS'));
      

      这首先需要这一行,以及您最初的“set”语句:

      set serveroutput on size 1000000;
      

      如果你在声明/开始/结束块中做一些事情,你可能想要这个:

      dbms_output.enable;
      

      这可能会改变你的输出。

      【讨论】:

        猜你喜欢
        • 2017-11-04
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2013-03-14
        • 2011-05-10
        • 2021-07-19
        • 2018-08-10
        相关资源
        最近更新 更多