从bash运行Oracle脚本

时间:2013-06-27 06:05:03

标签: oracle bash sqlplus

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

任何人都可以建议我一个更好的方法。

我的代码

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

3 个答案:

答案 0 :(得分:3)

尝试使用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"

答案 1 :(得分:2)

怎么样

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

答案 2 :(得分:2)

过去,我使用过dbms_output.put_line

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

首先需要此行,并使用您的初始“设置”语句:

set serveroutput on size 1000000;

如果您在声明/开始/结束块中执行操作,您可能需要这样:

dbms_output.enable;

这可能会改变您的输出。