编写 Oracle 过程以接受 Select 中使用的项目列表

发布于 2024-10-10 07:59:39 字数 510 浏览 0 评论 0原文

有一些提示似乎让我接近这里,但由于一些独特的问题,我希望这个问题足够独特,值得单独发布。

首先,这就是我所拥有的。我有一个返回标准 REF CURSOR 的 Oracle 过程,并且此 REF CURSOR 被传回我的应用程序。 REF CURSOR 是查找 ID 的列表。

然后我想获取这个列表并将其带到另一个数据存储中并在 select 语句中使用它。通过循环 REF CURSOR 绝对可以实现这一点,但我希望避免这种情况。我更愿意能够编写 SELECT...WHERE Lookup_id IN result_ref_cursorSELECT...WHERE EXISTS...

首先这是可能的还是我应该尝试一个不太优雅的解决方案?如果可能的话,有什么关于我应该开始寻找的提示吗?

我对 Oracle 比较陌生,但总体来说在 RDBM 方面经验丰富,所以请随意通过我的一些链接,我可以学习。非常感谢

There have been a couple of hints that seem to have gotten me close here, but with some unique issues, I'm hoping this question is distinguishing enough to merit its own posting.

For starters here's what I have. I have an Oracle procedure that returns a standard REF CURSOR, and this REF CURSOR is passed back to my application. The REF CURSOR is a list of lookup IDs.

I then want to take this list and bring it to another data store and use it in a select statement. It will absolutely be possible to accomplish this by looping through the REF CURSOR, but I'm hoping to avoid that. I would much rather be able to write a SELECT...WHERE lookup_id IN result_ref_cursor OR SELECT...WHERE EXISTS...

First is this possible or should I just try a less than elegant solution? If it is possible, any hints as to where I should get started looking?

I'm relatively new to Oracle, but fairly experienced in RDBMs in general, so feel free to just through some links at me and I can study up. Much appreciated

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

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

发布评论

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

评论(2

蓝眸 2024-10-17 07:59:39

为什么库罗什没有把他的回应作为“答案”,我不知道。

因此,您要做的就是定义一个 SQL 类型,它描述引用游标的一行输出,以及一个 SQL 类型,它是前一个表。然后,您将创建一个管道函数,该函数返回引用游标返回的行。然后可以在标准 SQL 中使用该函数。我借用 就这个问题询问汤姆

create or replace type myLookupId as object ( id int)
/

create or replace type myLookupIdTable as table of myLookupId
/

create or replace function f return myLookupIdTable PIPELINED is
  l_data myLookupId;
  l_id number;
  p_cursor SYS_REFCURSOR;
begin
  p_cursor := function_returning_ref_cursor();
  loop
    fetch p_cursor into l_id;
    exit when p_cursor%notfound;
    l_data := myLookupId( l_id );
    pipe row (l_data);
  end loop;
 return;
end;
/

现在是一个示例查询...

SELECT  * 
FROM    SOME_TABLE
WHERE   lookup_id in (SELECT ID FROM table(f));

抱歉,如果代码不完全正确,我现在没有要测试的数据库。

Why kurosch didn't put his response as an "answer" I'll have no idea.

So, what you do is define a SQL type which describes one row of the output of the ref cursor, and also a SQL type which is a table of the previous. Then, you'll create a pipelined function which returns the rows returned by the ref cursor. This function can then be used in a standard SQL. I'm borrowing from Ask Tom on this one.

create or replace type myLookupId as object ( id int)
/

create or replace type myLookupIdTable as table of myLookupId
/

create or replace function f return myLookupIdTable PIPELINED is
  l_data myLookupId;
  l_id number;
  p_cursor SYS_REFCURSOR;
begin
  p_cursor := function_returning_ref_cursor();
  loop
    fetch p_cursor into l_id;
    exit when p_cursor%notfound;
    l_data := myLookupId( l_id );
    pipe row (l_data);
  end loop;
 return;
end;
/

And now a sample query...

SELECT  * 
FROM    SOME_TABLE
WHERE   lookup_id in (SELECT ID FROM table(f));

Sorry if the code isn't exactly right, I don't have the DB to test right now.

め可乐爱微笑 2024-10-17 07:59:39

您可以采用多种方向,但我对您想要的具体解决方案进行了搜索,似乎没有人经常这样做来出现在那里。您可以做的就是搜索 oracle metalink - 这通常非常擅长找到晦涩难懂的答案。 (虽然您确实需要服务协议 - 刚刚发现我的已过期:( )

其他可能的解决方案:

在数据存储之间创建链接,以便您可以直接在 plsql 中进行选择

在 Java 中创建一个循环遍历它的函数至少,这看起来会更漂亮一点,

否则,REF CURSOR 需要来回移动 - 我不知道如何将 REF CURSOR 的结果通过管道传输到在另一个中查询而不循环遍历它。

There are several directions you could go with this, but I did a search on the specific solution you want and it seems like no-one has done it often enough to show up there. What you can do is search the oracle metalink - that is usually really good at finding obscure answers. (Though you do need a service agreement - just found out that mine expired :( )

Other possible solutions:

Create a link between the data stores so that you can do the select in the plsql directly

Create a function in Java that loops through it for you to create the string for the query. This will look a little more pretty at least.

Otherwise, REF CURSOR's need to go back and forth - I don't know how you can pipe the results of the REF CURSOR in one connection to the query in another without looping through it.

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