如何从嵌套 Oracle 过程中检索值?
我遇到了一个棘手的 Oracle 问题。我正在尝试选择一组数据,我们将其称为项目。对于每个项目,我想调用另一个过程并返回一个库存项目。我有两项手术,我不确定如何执行。
如何从嵌套过程中检索值?
如何以 SYS_REFCURSOR 的形式返回这些检索到的值?
我在这里的尝试是将 spSelect_Inv_Search 的结果放入名为 ITEMS_TABLE 的嵌套表中。这是行不通的。
下面的代码
PROCEDURE SPSELECT_ITEM (IO_CURSOR OUT SYS_REFCURSOR)
AS
MY_CURSOR SYS_REFCURSOR;
TYPE ITEM_TYPE IS TABLE OF ITEMS.ITEM_NO%TYPE;
ITEM_TABLE ITEM_TYPE := ITEM_TYPE();
CURSOR ITEMS_CURSOR IS
SELECT ITEM_NO
FROM ITEMS;
V_COUNTER INTEGER := 0;
BEGIN
FOR ITEM_REC IN ITEM_CURSOR LOOP
V_COUNTER := V_COUNTER + 1;
ITEM_TABLE.EXTEND;
ITEM_TABLE(V_COUNTER) := spSelect_Inv_Search(ITEM_REC.ITEM_NO, MY_CURSOR);
END LOOP;
END SPSELECT_ITEMS;
非常感谢任何帮助,谢谢。
I have kind of a tricky Oracle problem. I am trying to select one set of data, we'll call items. For each item I want to call another procedure and return an Inventory Item. I have two operations I am not sure on how to perform.
How do I retrieve a value from the nested procedure?
How do I return those retrieved values in the form of SYS_REFCURSOR?
My attempt here was to put the results from spSelect_Inv_Search into a nested table called ITEMS_TABLE. This is not working.
Code below
PROCEDURE SPSELECT_ITEM (IO_CURSOR OUT SYS_REFCURSOR)
AS
MY_CURSOR SYS_REFCURSOR;
TYPE ITEM_TYPE IS TABLE OF ITEMS.ITEM_NO%TYPE;
ITEM_TABLE ITEM_TYPE := ITEM_TYPE();
CURSOR ITEMS_CURSOR IS
SELECT ITEM_NO
FROM ITEMS;
V_COUNTER INTEGER := 0;
BEGIN
FOR ITEM_REC IN ITEM_CURSOR LOOP
V_COUNTER := V_COUNTER + 1;
ITEM_TABLE.EXTEND;
ITEM_TABLE(V_COUNTER) := spSelect_Inv_Search(ITEM_REC.ITEM_NO, MY_CURSOR);
END LOOP;
END SPSELECT_ITEMS;
Any help is appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎想要将未知数量的
SYS_REFCURSOR
结果集合并为一个大结果集。如果您知道从spSelect_Inv_Search
返回的游标的结构,您可以使用中间管道函数来完成此操作。类型可以在此处定义,它们不必在 SQL 级别,因为您不需要在包外部引用它们。
然后执行,尽管中间阶段使用了类型,但您可以在包外部执行此操作,您可以在 SQL*Plus 或 SQL Developer 中执行此操作以进行测试:
对于我的数据库,这给出:
这显然是非常人为的,就像您所做的那样这是一个单一的查询,但我假设您的内部过程需要做一些更复杂的事情。
You seem to be wanting to merge an unknown number of
SYS_REFCURSOR
result sets into one big one. If you know the structure of the cursor returned fromspSelect_Inv_Search
you can do this with an intermediate pipelined function.The types can be defined here, they don't have to be at SQL level as you won't ever need to reference them outside the package.
Then to execute, which you can do outside the package despite the types used for the intermediate stage, you can do this to test in SQL*Plus or SQL Developer:
For my database this gives:
This is obviously very contrived as you'd do this as a single query, but I'm assuming your inner procedure needs to do something more complicated.
为了回答有关如何调用
spSelect_Inv_Search
的问题,我需要知道该子程序的签名。您已将其描述为过程,但试图将其作为函数调用。是哪一个?它有什么返回值和/或 OUT 模式参数?要从上述过程返回打开的 REF CURSOR,首先需要在架构级别(使用 CREATE TYPE 语句)而不是在 PL/SQL 代码中声明嵌套表类型。然后,在填充嵌套表后,您可以像这样打开游标。
(顺便说一下,我自己将类型的名称从
ITEM_TYPE
更改为ITEM_TABLE_TYPE
。)In to answer your question about how to call
spSelect_Inv_Search
, I'd need to know the signature of that subprogram. You've described it as a procedure but you're trying to call it as a function. Which is it? What return value and/or OUT-mode parameters does it have?To return an open REF CURSOR from the above procedure, first the nested table type needs to be declared at the schema level (using a
CREATE TYPE
statement) instead of in the PL/SQL code. Then you can open the cursor like so, after populating the nested table.(And by the way, I would change the name of the type from
ITEM_TYPE
toITEM_TABLE_TYPE
, myself.)