简体   繁体   English

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

[英]Oracle PLSQL fetch multiple row/column value of number data type into a collection of number type?

We have a table that was designed to have multiple columns with number data types that stores a primary key from another table.我们有一个表,该表被设计为具有多个数字数据类型的列,这些列存储来自另一个表的主键。 Each row has a unique organization id.每行都有一个唯一的组织 ID。 So the structure looks like this.所以结构看起来像这样。

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
/

Then I try to do it like this but it doesn't work since it is expecting a collection of emp_ids然后我尝试这样做,但它不起作用,因为它需要一组 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

I also tried without the bulk collect but again it won't work since it is expecting only 1 record whereas I have 4.我也尝试过不使用批量收集,但它再次不起作用,因为它只需要 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

I know this can via union or thru loop but I was wondering if there is a simpler way to do it.我知道这可以通过 union 或 thru loop 但我想知道是否有更简单的方法来做到这一点。

Reason I'm doing this is because later on I need to join this with another table.我这样做的原因是因为稍后我需要将它与另一个表连接起来。 Kind of like this.有点像这样。

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

For reference, here's our database details:作为参考,这是我们的数据库详细信息:

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   

                               

Appreciate any feedback.感谢任何反馈。

As far as I know, we need to create an object type first.据我所知,我们需要先创建一个 object 类型。

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;

And this should work:这应该有效:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM