从 oracle 中的表/数组中插入和选择

发布于 2024-09-16 09:12:38 字数 916 浏览 2 评论 0原文

我有一个相当大的 SQL 语句,它返回 id 列表。我需要这个 id-list 作为其他语句的基础。像这样:

open crs_result1 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
open crs_result2 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
open crs_result3 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
...

当然,我不想每次为不同的选择选择整个 id 列表。

所以,我的想法是使用表/数组:

TYPE gt_result_rec IS RECORD
(
    id NUMBER
);
TYPE gt_result_tab IS TABLE OF gt_result_rec INDEX BY BINARY_INTEGER;

t_results gt_result_tab;

execute immediate 'insert into t_results select <ids> from <base_statement>';

而不是将其用于所有其他语句:

open crs_result1 for ' select * from ... where id in ( select id from t_results ) ';
...

但这实际上不起作用。

有谁知道这个问题或有更好的解决方案吗?

I have a pretty big SQL statement which returns a list of id's. I need this id-list as base for other statements. Like this:

open crs_result1 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
open crs_result2 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
open crs_result3 for ' select * from ... where id in ( select <ids> from <base_statement> ) ';
...

Of course I don't want to select the whole id-list each time for the different selects.

So, my idea was to use a table/array:

TYPE gt_result_rec IS RECORD
(
    id NUMBER
);
TYPE gt_result_tab IS TABLE OF gt_result_rec INDEX BY BINARY_INTEGER;

t_results gt_result_tab;

execute immediate 'insert into t_results select <ids> from <base_statement>';

And than use it for all other statements:

open crs_result1 for ' select * from ... where id in ( select id from t_results ) ';
...

But this doesn't really work.

Does anyone know the problem OR has a better solution for this?

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

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

发布评论

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

评论(1

带刺的爱情 2024-09-23 09:12:38

使用临时表可以实现类似的事情,如下所示:

create global temporary table temp_ids(id number) on commit preserve rows ;

...比插入数据:

execute immediate 'insert into temp_ids(id) select id from <big statement>';
execute immediate 'insert into temp_ids(id) select id from <other big statement>';
execute immediate 'insert into temp_ids(id) select id from <other big statement>';

..最后您可以使用您的想法:

open crs_result1 for ' select * from ... where id in ( select id from temp_ids ) ';

使用 TEPORARY TABLES 达到 EXECUTE IMMEDIATE 将从与其他 PL/SQL 相同的上下文中获取数据代码运行。

如果要使用记录表(表/数组),则需要在 PACKAGE(标头,而不是正文!)中声明此数组,因此该字段从 EXECUTE IMMEDIATE 的上下文中可见。 ARRAY 必须是 PUBLIC 可见的。

A similar thing can be achieved using TEMPORARY tables, like this:

create global temporary table temp_ids(id number) on commit preserve rows ;

...than insert data:

execute immediate 'insert into temp_ids(id) select id from <big statement>';
execute immediate 'insert into temp_ids(id) select id from <other big statement>';
execute immediate 'insert into temp_ids(id) select id from <other big statement>';

..finally you can use your idea:

open crs_result1 for ' select * from ... where id in ( select id from temp_ids ) ';

Using TEPORARY TABLES reach that EXECUTE IMMEDIATE will take data from the same context in which your other PL/SQL code run.

If you want to use TABLE OF RECORDS (table/array), you will need declare this ARRAY in the PACKAGE (header, not BODY!), so the field will be visible from the context of the EXECUTE IMMEDIATE. ARRAY must be PUBLIC visible.

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