Oracle PLSQL是否将数字数据类型的多行/列值提取到数字类型的集合中



我们有一个表,它被设计为具有多个列,这些列的数字数据类型存储另一个表中的主键。每一行都有一个唯一的组织id。所以结构看起来是这样的。

create table matrix_table (
id number primary key
, emp_id1 number
, emp_id2 number
, emp_id3 number
, emp_id4 number
)
/
insert into matrix_table values ( 1, 100, 101, 102, 103 )
/
insert into matrix_table values ( 2, 200, 201, 202, 203 )
/
insert into matrix_table values ( 3, 300, 301, 302, 303 )
/
insert into matrix_table values ( 4, 400, 401, 402, 403 )
/
create type emp_ids is table of number
/

然后我试着这样做,但它不起作用,因为它期望一个emp_ids 集合

declare
l_emp_ids emp_ids := emp_ids();
begin
select emp_ids(emp_id1, emp_id2, emp_id3, emp_id4)
bulk collect
into l_emp_ids
from matrix_table;
end;
ORA-06550: line 4, column 12: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got APPS.EMP_IDS
ORA-06550: line 4, column 5: PL/SQL: SQL Statement ignored

我也尝试过不使用批量收集,但它再次不起作用,因为它只期望1张唱片,而我有4张。

declare
l_emp_ids emp_ids := emp_ids();
begin
select emp_ids(emp_id1, emp_id2, emp_id3, emp_id4)
into l_emp_ids
from matrix_table;
end;
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 4

我知道这可以通过联合或通过循环,但我想知道是否有更简单的方法

我这样做的原因是,稍后我需要将此与另一个表连接起来。有点像这样。

select e.*
from employees e
, table ( emp_ids_var ) v
where e.emp_id = v.column_value

供参考,以下是我们的数据库详细信息:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production                                      
CORE    12.1.0.2.0  Production                                                  
TNS for Linux: Version 12.1.0.2.0 - Production                              
NLSRTL Version 12.1.0.2.0 - Production   

感谢任何反馈。

据我所知,我们需要首先创建一个对象类型。

create type obj_ids is object (
emp_id1 number
, emp_id2 number
, emp_id3 number
, emp_id4 number
);
create type emp_ids is table of obj_ids;

这应该有效:

declare
l_emp_ids emp_ids := emp_ids();
begin
select obj_ids(emp_id1, emp_id2, emp_id3, emp_id4)
bulk collect
into l_emp_ids
from matrix_table;

for i in 1..l_emp_ids.count
loop
dbms_output.put_line (l_emp_ids(i).emp_id1);
end loop;
end;

最新更新