【问题标题】:How can I fetch the data from the SYS_REFCURSOR from one stored proc and use it in another?如何从一个存储过程中从 SYS_REFCURSOR 中获取数据并在另一个过程中使用它?
【发布时间】:2012-04-19 12:39:24
【问题描述】:

我有一个存储过程,其基本布局如下,它返回一个 sys_refcursor 作为结果集。 (从技术上讲,它会重复四个,但为了清楚起见,我只说一个)。结果集是从临时表中选择的。

procedure aProcedure
( C1 in out sys_refcursor
) is
begin
--populate Temp_Table here with a stored proc call;
OPEN C1 FOR
SELECT Cols
FROM TEMP_TABLE;

我需要使用不同的存储过程将此结果集从 C1 插入到永久表中。这是可行的还是我需要重新构建结果集?

我已经能够找到有关在 oracle 之外使用游标和结果集但在其内部使用它们的信息。

我知道从第一个存储过程中进行插入可能是有意义的,但这并不是我真正需要的方式。永久保存结果集是一项可选要求。

感谢您提供任何有用的信息。

【问题讨论】:

  • 为什么需要分2个程序来做这个?为什么不是 1?

标签: sql oracle stored-procedures database-cursor


【解决方案1】:

假设调用者知道aProcedure正在打开的游标的结构,你可以这样做。

declare
  l_rc sys_refcursor;
  l_rec temp_table%rowtype;
begin
  aProcedure( l_rc );
  loop
    fetch l_rc
     into l_rec;
    exit when l_rc%notfound;

    dbms_output.put_line( l_rec.col1 );
  end loop;
  close l_rc;
end;
/

如果您无法获取记录类型,您还可以获取许多其他标量局部变量(数量和类型必须与aProcedure 在其SELECT 中指定的列的数量和类型相匹配列表)。在我的例子中,我定义了aProcedure 来返回两个数字列

declare
  l_rc sys_refcursor;
  l_col1 number;
  l_col2 number;
begin
  aProcedure( l_rc );
  loop
    fetch l_rc
     into l_col1, l_col2;
    exit when l_rc%notfound;
    dbms_output.put_line( l_col1 );
  end loop;
  close l_rc;
end;

【讨论】:

  • 感谢您抽出宝贵时间回答贾斯汀这个问题。尽管我有四个不同的游标从两个临时表和一个需要收集并插入到另一个表中的视图生成数据,但这是有道理的。我的新手大脑需要一些时间才能正确实现它。这绝对让我朝着正确的方向前进。
猜你喜欢
  • 2011-04-26
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2014-09-17
相关资源
最近更新 更多