DML子查询中的NESTED表

问题描述 投票:1回答:1

使用Oracle 12c EE,如何在DML子查询中使用PLSQL包类型而不引起异常 "ORA-00902: invalid datatype"?

示例模式

--PL/SQL package types.
create or replace package test_pkg as
    TYPE type_record IS RECORD(
        column1        NUMBER,
        column2        NUMBER,
        column3        NUMBER);

    TYPE type_table IS TABLE OF type_record;
end;
/

--For comparison, the same types but as SQL objects.
CREATE OR REPLACE TYPE type_record IS OBJECT(
    column1        NUMBER,
    column2        NUMBER,
    column3        NUMBER);

CREATE OR REPLACE TYPE type_table IS TABLE OF type_record;

--Table for testing DML.
create table tableX(a number);

SQL SELECT中的PLSQL类型 - WORKS

从PLSQL类型转换为SQL,对于SELECTS来说,可以正常运行。下面的代码运行正常。

declare
    vt test_pkg.type_table;
    v_count number;
begin
    select count(*)
    into v_count
    from dual
    where not exists(select column1 from table(vt));
end;
/

在SQL UPDATE中的PLSQL类型 - 失败。

但是在DML语句中使用相同的类型和子查询会引起异常。"ORA-00902: invalid datatypeORA-06512: at line 4".

declare
    vt test_pkg.type_table;
begin
    update tableX set a = 1
    where not exists (select column1 from table(vt));
end;
/

SQL UPDATE中的SQL类型--WORKS

比较而言,在子查询中使用SQL对象在DML中工作得很好。

declare
    vt type_table;
begin
    update tableX set a = 1
    where not exists (select column1 from table(vt));
end;
/

为每个查询创建SQL对象是一个变通的方法,但那会创建很多不必要的模式对象。有没有办法让PLSQL包类型在DML子查询中工作?

sql oracle plsql oracle12c
1个回答
0
投票

我想这是一个观点问题,因为我使用db级别的类型,因为(我认为)那是保持代码干净的方法。但是,这只是一种不同的方式来看待它。 然而,你要找的东西,至少在这个案例中,并不难。你已经创建了一个使用这些定义的包。你可以把这些定义移到包的规范中。那么就几乎不需要修改代码了。而且这些定义仍然可以在包本身之外使用。

create or replace package pkg_t1 as  
   type type_record is record(
      column1        number,
      column2        number,
      column3        number);

   type type_table is table of type_record;

   function build_type_records(rows_to_build in integer) 
     return type_table;

   function build_pipe_records(rows_to_build in integer) 
     return type_table 
     pipelined;    

end pkg_t1;
/

create or replace package body pkg_t1 as

   function build_type_records(rows_to_build in integer) 
   return  type_table 
   is  
      v_type type_table := type_table();
   begin 
      for i in 1 .. rows_to_build 
      loop 
         v_type.extend;      
         v_type(i).column1 := trunc(dbms_random.value(1,100)); 
         v_type(i).column2 := trunc(dbms_random.value(100,500));
         v_type(i).column3 := dbms_random.value();
      end loop ;

     return  v_type;
   end build_type_records; 

  function build_pipe_records(rows_to_build in integer) 
    return type_table 
    pipelined 
  is
    v_rec type_table;
  begin
    v_rec:= build_type_records(rows_to_build); 
    for i in 1 .. v_rec.count
    loop
      pipe row (v_rec(i));
    end loop; 
  end build_pipe_records;

end pkg_t1;
/ 

declare 
  tt pkg_t1.type_table; 
  num_of_rows integer := &Number_of_rows;
begin
  tt := pkg_t1.build_type_records( num_of_rows );
  for i in 1 .. tt.count
  loop
      dbms_output.put_line( 'Result: '
                            || 'Column1==>' || tt(i).column1 || ', ' 
                            || 'Column2==>' || tt(i).column2 || ', '
                            || 'Column3==>' || tt(i).column3
                          ) ; 
  end loop; 
end ; 
/

select * from table(pkg_t1.build_pipe_records(&Rows_Desirded));
© www.soinside.com 2019 - 2024. All rights reserved.