2012-07-23 16 views
1

我已被授予對Oracle數據庫的讀取訪問權限以獲取我自己的數據庫的數據。 DBA給了我一個存儲過程,他保證我是我所需要的,但是我一直無法從Ruby運行它。如何在Ruby中運行Oracle存儲過程

我安裝了ruby-oci8 gem和oracle即時客戶端。

以下是我迄今爲止所管理的內容。

require 'oci8' 
conn = OCI8.new('user','pass','//remoteora1:1521/xxxx') 
=> #<OCI8::RWHRUBY> 
cursor = conn.parse("call REPOSITORY.GET_PMI_ADT('722833', 'W', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)") 
=> #<OCI8::Cursor:0x56f4d50> 

而這就是我卡住的地方。我有這個OCI8 :: Cursor對象,但我不知道該如何處理它。它應該返回一大堆結果(空值在查詢中),但我什麼也沒有得到。運行帶有和沒有參數的cursor.exec(我不知道我需要什麼參數)給我錯誤。

cursor.exec給

OCIError: ORA-06553: PLS-: 
ORA-06553: PLS-: 
ORA-06553: PLS-: 
ORA-06553: PLS-306: wrong number or typ 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 
ORA-06553: PLS-306: wrong number or type of arguments in call to 'GET_PMI_ADT' 

等等

cursor.fetch給

OCIError: ORA-24338: statement handle not executed 

沒有人有任何的想法嗎?我完全沉浸在我的頭腦中。

此處爲所有仍在觀看的人提供更新。如果我將存儲過程更改爲

BEGIN REPOSITORY.GET_PMI_ADT('722833', 'W', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); END; 

我現在得到錯誤;

OCIERROR: ORA-06550: line 1, column 45: 
PLS-00363: expression ' NULL' cannot be used as an assignment target 

這是否確認存儲過程不正確?有什麼明顯的做法來糾正它?

+0

對不起,我應該包括這一點。在上面添加。 – brad 2012-07-23 08:05:34

+0

您是否嘗試過在Oracle中執行確切的代碼行?似乎您試圖調用的存儲過程存在問題。 – Strelok 2012-07-23 08:11:45

+0

我沒有訪問Oracle數據庫的權限,我向DBA保證他的存儲過程不可能有任何問題.....但我有我的懷疑。 – brad 2012-07-23 08:14:14

回答

3

這應有助於:

http://rubyforge.org/forum/forum.php?thread_id=11295&forum_id=1078

樣品是如下:

MSI是在變量的地位和remaining_credit是OUT變量

cursor = conn.parse ('begin ROAMFLEX.GETMSISDNSTATUS(:msi, :status, :remaining_credit); end;') 
cursor.bind_param(':msi', '250979923') 
cursor.bind_param(':status', nil, String, 20) 
cursor.bind_param(':remaining_credit', nil, String, 50) 
cursor.exec() 
puts cursor[':status'] 
puts cursor[':remaining_credit_amount'] 
+0

謝謝這麼多!這個例子正是我需要修改我的代碼以使其工作。在放棄這一年之後,我現在能夠使其工作。 – brad 2013-11-15 01:41:26

1

您正在接受

PLS-00363: expression ' NULL' cannot be used as an assignment target 

錯誤意味着,在存儲過程中,某些參數被定義爲IN OUT,這意味着它們必須是變量。使用一個參數來保存一個返回值是很常見的,這個返回值指示一個過程是否通過或者失敗,儘管使用一個可以返回布爾值true/false來表示成功的函數更爲常見。

你需要給DBA你所得到的錯誤消息,並詢問你正在調用哪一個要告訴你哪些變量要傳遞的程序一個完整簽名的IN/OUT的。任何只有IN纔可以傳遞一個常量或NULL,而那些OUT或IN OUT需要是變量,你可能需要根據你在這些變量中返回的內容做一些事情。

1
# PL/SQL records or object type parameters should be passed as Hash 
# test_full_name is procedure name in oracle 
# p_employee holds parameter list 
#employee_id is first param defined in stored procedure 

require 'ruby-plsql' 

p_employee = { :employee_id => 1, :first_name => 'First', :last_name => 'Last', :hire_date => Time.local(2000,01,31) } 
plsql.test_full_name(p_employee) #plsql should hold connection object