2012-06-23 48 views
1

感謝您的時間!ruby​​ oci8生成oracle AWR報告

我想用ruby oci8連接oracle數據庫來生成AWR報告。

當我做到這一點通過命令行,代碼是這樣的:

sqlplus sys/[email protected]/load as sysdba 
SQL> define num_days = '' 
SQL> define report_type = "html" 
SQL> define begin_snap = 100 
SQL> define end_snap = 101 
SQL> define report_name = C:\tttt.html 
SQL> @?\rdbms\admin\awrrpt.sql 

我只是想使用Ruby實現自動化作業。我谷歌它並找到oci8可能會有所幫助。所以我只是這樣形成我的代碼:

require 'oci8' 
onn = OCI8.new('sys/[email protected]/load as sysdba') 
conn.exec("define num_days = '';") 
conn.exec('define report_type="html"') 
onn.exec('define begin_snap = 100') 
conn.exec('define end_snap = 101') 
conn.exec('define report_name = C:\tttt.html') 
conn.exec('@?\rdbms\admin\awrrpt.sql') { |r| puts r} 

當我在cmd中運行它時,它失敗。

失敗的消息是:

Warning: NLS_LANG is not set. fallback to US7ASCII. 

    stmt.c:253:in oci8lib_191.so: ORA-00900: invalid SQL statement (OCIError) 

    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:474:in `exec' 

    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:282:in `exec_internal' 

    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:275:in `exec' 

    from automate_awr.rb:4:in `<main>' 

此外,我可以成功登錄到Oracle使用OCI8執行SELECT語句。

我在哪裏錯了?

在此先感謝!

回答

1

主要問題是您在應該在服務器端執行的查詢和要在客戶端執行的sqlplus命令之間混淆。

define和@命令是sqlplus命令,在客戶端執行。他們不會被髮送到Oracle。通過oci8模塊,Ruby可以使用OCI連接到Oracle。它可以發送任何查詢到服務器,但當然,它將無法運行sqlplus命令。

「?\ rdbms \ admin」中的腳本應該從sqlplus執行。如果你真的需要從Ruby運行它,我會建議使用Ruby Open3模塊來分叉一個sqlplus進程並使用管道提供輸入參數。

應該可以使用基於下面的代碼的東西:

commands = " 
    define num_days = '' 
    define report_type = 'html' 
    define begin_snap = 100 
    define end_snap = 101 
    define report_name = C:\tttt.html 
    @?\rdbms\admin\awrrpt.sql 
" 
res, s = Open3.capture2e("sqlplus -S sys/[email protected]/load as sysdba", :stdin_data=>commands) 
+0

非常感謝! 我很清楚我現在錯在哪裏。我會首先調查popen3,並在完成任務後給出任務。 – mCY