2010-08-27 50 views
2

我有一个非常大的SQL语句,它返回一个id列表。我需要这个id列表作为其他语句的基础。像这样:插入并从oracle中的表/数组中选择

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

但是,这并不真正发挥作用。

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

回答

2

类似的事情可以使用临时表来实现,这样的:

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

..finally你可以用你的想法:

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

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

如果你想使用TABLE OF RECORDS(table/array),你需要在PACKAGE(header,not BODY!)中声明这个ARRAY,所以这个字段在EXECUTE IMMEDIATE的上下文中是可见的。 ARRAY必须公开可见。

相关问题