【问题标题】:Insert and select from a table/array in oracle在 oracle 中从表/数组中插入和选择
【发布时间】:2011-04-04 17:32:10
【问题描述】:

我有一个相当大的 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-list。

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

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 ) ';
...

但这并没有真正起作用。

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

【问题讨论】:

    标签: arrays oracle select insert


    【解决方案1】:

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

    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 代码的同一上下文中获取数据。

    如果您想使用 TABLE OF RECORDS(表/数组),您需要在 PACKAGE 中声明此 ARRAY(标题,而不是 BODY!),因此该字段将在 EXECUTE IMMEDIATE 的上下文中可见。 ARRAY 必须是 PUBLIC 可见的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多