如何在sql查询中使用plsql表类型?
让我先说我对其他的方法持开放态度,但现在,这就是我所能想到的 我在package.procedure中工作,我使用表类型作为数组/列表来存储符合搜索条件的记录ID。编译完列表后,我想打开一个游标,从列表中记录ID所在的表中进行选择 数据结构:如何在sql查询中使用plsql表类型?,plsql,Plsql,让我先说我对其他的方法持开放态度,但现在,这就是我所能想到的 我在package.procedure中工作,我使用表类型作为数组/列表来存储符合搜索条件的记录ID。编译完列表后,我想打开一个游标,从列表中记录ID所在的表中进行选择 数据结构: TYPE i_array IS TABLE OF t_table.id_column%TYPE INDEX BY PLS_INTEGER; lt_results i_array; ln_count pls_integer; 填充列表: ln_co
TYPE i_array IS TABLE OF t_table.id_column%TYPE INDEX BY PLS_INTEGER;
lt_results i_array;
ln_count pls_integer;
填充列表:
ln_count := 0;
for recs in (select id_column from t_child where other_column = ls_criteria)
loop
ln_count := ln_count + 1;
lt_results(ln_count);
end loop;
打开光标和访问列表:
open cur for
select col_a,
col_b,
col_c
from t_table
where id_column in (select lt_results(level)
from dual
connect by level <= ln_count);
如果使用Oracle 12C,则可以在包中使用嵌套表集合,如下所示:
create or replace package test is
TYPE i_array IS TABLE OF t_child.id_column%TYPE;
procedure run;
end;
create or replace package body test is
procedure run is
lt_results i_array := i_array();
cur sys_refcursor;
begin
for r in (select id_column from t_child where other_column = ls_criteria) loop
lt_results.extend;
lt_results(lt_results.count) := r.id_column ;
end loop;
open cur for
select col_a,
col_b,
col_c
from t_table
where id_column in (select column_value from table(lt_results));
end run;
end;
在12C之前,类型i_数组需要位于数据库中:
create type i_array is table of varchar2(10); -- For example
您可以使用类似SYS.KU$\VCNT的脚本,而不是创建自己的脚本
顺便说一句,这是:
for r in (select id_column from t_child where other_column = ls_criteria) loop
lt_results.extend;
lt_results(lt_results.count) := r.id_column ;
end loop;
可通过以下方式更有效地替换:
select id_column
bulk collect into lt_results
from t_child where other_column = ls_criteria;
忘记了最重要的部分。当前设置给出ORA-01403:在打开的光标行未找到数据。我确信错误是由where子句中的列表访问引起的。最好使用bulk collect填充数组。此外,如果要在SQL中使用数组,则必须在架构级别创建类型-即创建或替换类型。。。。更好的是,为什么要使用数组呢?为什么不从t_child where中选择id_列中的where id_列。。。在cur ref光标中,粘滞点不是收集点。。它正在使用返回游标的where子句中的列表。您不能。您使用的是什么版本的Oracle?12C允许在SQL中使用嵌套表集合,而不是按包中声明的数组进行索引。我不做通常的where in操作,因为ls_条件是文本输入的子字符串。[亚特兰大,星期四]每个字符串都将用作搜索条件。匹配的记录将添加到返回光标的列表中