在 R 中调用 Oracle 存储过程 - 如何获取结果集?

发布于 2024-12-06 15:38:15 字数 490 浏览 0 评论 0原文

寻找使用 R 调用 Oracle 存储过程并返回结果集的示例。

我使用 RJDBC 库 dbGetQuery 来调用 Sybase procs 并将结果指向变量,这对于 Oracle select stmts 的工作原理相同。但是,我不知道如何让它从 Oracle 存储过程(即从 sys_refcursor 输出参数)返回 Oracle 结果集。我找到的从 Oracle 检索数据的唯一示例涉及“从表中选择列”。

在 google 中搜索后,我找到了“dbCallProc – 调用 SQL 存储过程”,这听起来很有希望,但我发现的每个参考文献都表明它“尚未实现”。 >"

有使用过程的指针或示例吗?非常赞赏。不知道为什么 Oracle 在检索结果集时总是遇到这样的挑战......

谢谢, Mike

更新:我举一个简单地调用 Oracle 存储过程的例子。 RJDBC 目前不支持 Oracle 过程吗?

Looking for an example for calling Oracle stored proc using R, and returning a result set.

I'm using RJDBC library, dbGetQuery to call Sybase procs and point the results to a variable, and this works the same for Oracle select stmts. However, I don't see how to get this to return Oracle result sets from an Oracle stored proc (i.e., from the sys_refcursor out param). The only examples I find for retrieving data from Oracle involve "select columns from table".

Searching in google was led me to "dbCallProc – Call an SQL stored procedure" which sounds very promising, but every ref I found to it indicates that it is "Not yet implemented."

Any pointers or examples for using procs? Greatly appreciated. Don't know why Oracle always has to be such a challenge for retrieving result sets....

Thanks,
Mike

UPDATE: I'd take an example that simply called an Oracle stored proc. Are Oracle procs simply not supported currently in RJDBC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-12-13 15:38:15

我无法专门帮助您使用 R,但您说您在调用使用 OUT 参数作为 sys_refcursors 的 Oracle 过程时遇到问题。您还指出此功能可能尚未实现。但是,您确实说您可以“从表中选择列”。

因此,我建议将过程更改为管道函数调用,然后执行简单的选择以从 Oracle 获取数据。一个小例子:

CREATE OR REPLACE package pkg1 as

  type t_my_rec is record
  (
    num my_table.num%type,
    val my_table.val%type
  );

  type t_my_tab is table of t_my_rec;

  function get_recs(i_rownum in number)
      return t_my_tab
      pipelined;

END pkg1;

包体:

create or replace package body pkg1 as

  function get_recs(i_rownum in number)
      return t_my_tab
      pipelined
  IS
    my_rec t_my_rec;
  begin

    -- get some data
    -- implement same business logic as in procedure
    for my_rec in (select num, val from my_table where rownum <= i_rownum)
    loop
      pipe row(my_rec);
    end loop;
    return; 

  end get_recs;

end pkg1;

用法:

select * from table(pkg1.get_recs(3));

或者:

select num, val from table(pkg1.get_recs(3));

这将返回 3 行数据,就像过程返回相同的数据一样。只有这样你才能从 select 语句中获取它(你似乎可以从 R 中处理它)。

希望有帮助。

I can't help you specifically with R, but you say you're having issues in calling Oracle procedures that use OUT params as sys_refcursors. You also indicate this ability may not be implemented yet. You do say, however, that you can "select columns from table" just fine.

So, I propose changing the procedures to pipelined function calls, and then doing a simple select to get your data from Oracle. A small example:

CREATE OR REPLACE package pkg1 as

  type t_my_rec is record
  (
    num my_table.num%type,
    val my_table.val%type
  );

  type t_my_tab is table of t_my_rec;

  function get_recs(i_rownum in number)
      return t_my_tab
      pipelined;

END pkg1;

The package body:

create or replace package body pkg1 as

  function get_recs(i_rownum in number)
      return t_my_tab
      pipelined
  IS
    my_rec t_my_rec;
  begin

    -- get some data
    -- implement same business logic as in procedure
    for my_rec in (select num, val from my_table where rownum <= i_rownum)
    loop
      pipe row(my_rec);
    end loop;
    return; 

  end get_recs;

end pkg1;

Usage:

select * from table(pkg1.get_recs(3));

Or:

select num, val from table(pkg1.get_recs(3));

This would return 3 rows of data, just as a procedure would return the same data. Only this way you can get it from a select statement (which you seem to be able to handle from R).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文