如何在Ruby中运行Oracle存储过程

我获得了对Oracle数据库的读访问权,以获取我自己的数据库的数据。 DBA给了我一个存储过程,他向我保证是我所需要的,但我还是无法从Ruby运行它。

我安装了ruby-oci8 gem和oracle即时客户端。

这是我到目前为止所管理的内容。

require 'oci8' conn = OCI8.new('user','pass','//remoteora1:1521/xxxx') => # 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对象,但我不知道该如何处理它。 它应该返回一大堆结果(其中null值在查询中)但我什么都没得到。 使用和不使用参数运行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 

这是否确认存储过程不正确? 有什么明显的事要做才能纠正吗?

这应该有所帮助:

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

样品如下:

msi处于IN变量状态,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'] 

您收到的错误

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

意味着在存储过程中,一些参数被定义为IN OUT,这意味着它们必须是变量。 使用参数来保存指示过程是通过还是失败的返回值是很常见的,尽管使用一个函数可以返回一个布尔值true / false来指示成功。

您需要向DBA提供您正在获取的错误消息,并要求为您正在调用的过程提供完整签名,该签名应该告诉您传入的变量是IN / OUT变量。 只有IN的任何一个都可以传递一个常量或NULL,而那些OUT或IN OUT需要是变量,你可能需要根据你在这些变量中得到的东西做一些事情。

 # 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