从 oracle 中的表/数组中插入和选择
我有一个相当大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用临时表可以实现类似的事情,如下所示:
...比插入数据:
..最后您可以使用您的想法:
使用 TEPORARY TABLES 达到 EXECUTE IMMEDIATE 将从与其他 PL/SQL 相同的上下文中获取数据代码运行。
如果要使用记录表(表/数组),则需要在 PACKAGE(标头,而不是正文!)中声明此数组,因此该字段从 EXECUTE IMMEDIATE 的上下文中可见。 ARRAY 必须是 PUBLIC 可见的。
A similar thing can be achieved using TEMPORARY tables, like this:
...than insert data:
..finally you can use your idea:
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.