简体   繁体   English

从bash运行Oracle脚本

[英]Running Oracle script from bash

My query basically is when I am trying to run multiple oracle scripts from bash how do I put comments in between the scripts. 我的查询基本上是当我尝试从bash运行多个oracle脚本时,如何在脚本之间添加注释。 I have tried using a workaround by selecting a string from dual. 我试图通过从双选择一个字符串尝试使用一种解决方法。 But the output formatting isn't very good. 但是输出格式不是很好。

Can anyone please suggest me a better way. 谁能建议我一个更好的方法。

My Code 我的密码

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

Output 输出量

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

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


***Running Script2***    

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

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

Expected Output 预期产量

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

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

Try using the PROMPT command of SQL*Plus: 尝试使用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

Output: 输出:

$ ./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"

怎么样

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

In the past, I've used dbms_output.put_line : 过去,我使用过dbms_output.put_line

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

This requires this line first, with your initial "set" statements: 这首先需要此行,并带有初始的“ set”语句:

set serveroutput on size 1000000;

And if you're doing stuff in a declare / begin / end block, you might want this: 如果您在声明/开始/结束块中进行操作,则可能需要这样做:

dbms_output.enable;

Which may change your output. 这可能会改变您的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM