【问题标题】:select from table with function using pipelined table functions and associative table使用流水线表函数和关联表从具有函数的表中选择
【发布时间】:2013-06-06 13:30:45
【问题描述】:

作为此选择的结果,我需要返回表格。传递给函数 opredelyaet 的参数,关联数据数组将从该函数中显示为表格:

select * from table(task_2.get_con_coll('save'));

我写了这段代码,对我来说它是正确的,我没有看到我错过了,或者它没有铺平

С创建一个对象

-- the creation of an array type
create or replace type con_coll_type is object(
    id        integer,
    user_name varchar2(255));

然后他创建了包

create or replace package task_2 is
  /*
  -- may need to be so?
      type con_coll_type is record(
        id        integer,
        user_name varchar(255));
  -- */
  --Create associative array
  type con_coll_t is table of con_coll_type index by varchar2(255);
  -- need using this   
  function get_con_coll(coll_name varchar2) return con_coll_t
    pipelined;

end task_2;

create or replace package body task_2 is

  function get_con_coll(coll_name varchar2) return con_coll_t
    pipelined is
    indx        varchar(255);
    coll_edit   con_coll_t;
    coll_delete con_coll_t;
    coll_save   con_coll_t;
  begin
-- Filling collection
    coll_edit(1) := con_coll_type(1, 'some_name_1');
    coll_edit(2) := con_coll_type(2, 'some_name_2');
    coll_delete(3) := con_coll_type(3, 'some_name_3');
    coll_delete(4) := con_coll_type(4, 'some_name_4');
    coll_save(5) := con_coll_type(5, 'some_name_5');
    coll_save(6) := con_coll_type(6, 'some_name_6'); 

-- If the parameter is passed to the function "Save" - ​​a collection of output
    if coll_name = 'save' then
      indx := coll_save.first;
      loop
        exit when indx is null;
      -- pipelined output
        pipe row(con_coll_type(coll_save(indx).id,
                               coll_save(indx).user_name));
        indx := coll_save.next(indx);
      end loop;
    end if;

  end get_con_coll;

end task_2;

我的代码有什么问题?我无法理解我错过了什么。

【问题讨论】:

  • 问题的症状是什么?
  • 你还没说怎么回事。你得到一个编译错误?出乎意料的输出?还有什么?
  • 我不记得号码了,但是以select * from table(task_2.get_con_coll('save')); 一个错误invalid datatype为准

标签: oracle function plsql associative-array pipelined-function


【解决方案1】:

这就是我想要的,也是我的成就

create or replace type con_coll_type is object(id integer,
                                               user_name varchar2(255));

create or replace type con_coll_t is table of con_coll_type;


create or replace package task_2 is

  type list_of_oper is table of con_coll_t index by varchar2(255);

  function get_con_coll(coll_name varchar2) return con_coll_t
    pipelined;

end task_2;

create or replace package body task_2 is

  function get_con_coll(coll_name varchar2) return con_coll_t
    pipelined is
    indx      varchar(255);
    indx_2    varchar(255);
    coll_main list_of_oper;
  begin

    coll_main('edit') := con_coll_t(con_coll_type(1, 'some_name_1'),
                                    con_coll_type(2, 'some_name_2'));
    coll_main('delete') := con_coll_t(con_coll_type(3, 'some_name_3'),
                                      con_coll_type(4, 'some_name_4'));
    coll_main('save') := con_coll_t(con_coll_type(5, 'some_name_5'),
                                    con_coll_type(6, 'some_name_6'));

    indx := coll_main.first;
    loop
      exit when indx is null;
      if indx = coll_name then
        indx_2 := coll_main(indx).first;
        loop
          exit when indx_2 is null;
                    pipe row(coll_main(indx)(indx_2));
          /*pipe row(con_coll_type(coll_main(indx)(indx_2).id,
coll_main(indx)(indx_2).user_name));*/
          indx_2 := coll_main(indx).next(indx_2);
        end loop;
      end if;
      indx := coll_main.next(indx);
    end loop;

    return;

  end get_con_coll;

end task_2;

select *
  from table(task_2.get_con_coll('edit'))
union all
select *
  from table(task_2.get_con_coll('delete'))
union all
select *
  from table(task_2.get_con_coll('save'));

【讨论】:

    【解决方案2】:

    首先,Table类型应该是包外的:
    create type con_coll_t is table of con_coll_type;

    而且,如果你的类型有默认值,你可以像这样实现它们:

      coll_delete con_coll_t;
      begin
        coll_delete := con_coll_t(con_coll_type(3, 'some_name_3'),
                                  con_coll_type(4, 'some_name_4'));
      end;
    

    话虽如此,我认为您的通用代码应该是这样的:

     -- the creation of an array type
     create or replace type con_coll_type is object(
         id        integer,
         user_name varchar2(255));
    
     create or replace type con_coll_t is table of con_coll_type;          
    
     create or replace package task_2 is
       function get_con_coll(coll_name varchar2) return con_coll_t
         pipelined;
     end task_2;
    
     create or replace package body task_2 is
    
       function get_con_coll(coll_name varchar2) return con_coll_t
         pipelined is
         indx        varchar(255);
         coll_edit   con_coll_t;
         coll_delete con_coll_t;
         coll_save   con_coll_t;
       begin
     -- Filling collection
         coll_edit    := con_coll_t( con_coll_type(1, 'some_name_1')
    
                                   , con_coll_type(2, 'some_name_2'));
         coll_delete  := con_coll_t( con_coll_type(3, 'some_name_3')
                                   , con_coll_type(4, 'some_name_4'));
         coll_save    := con_coll_t( con_coll_type(5, 'some_name_5')
                                   , con_coll_type(6, 'some_name_6'));
    
     -- If the parameter is passed to the function "Save" - ??a collection of output
         if coll_name = 'save' then
           indx := coll_save.first;
           loop
             exit when indx is null;
           -- pipelined output
             pipe row(con_coll_type(coll_save(indx).id,
                                    coll_save(indx).user_name));
             indx := coll_save.next(indx);
           end loop;
         end if;
    
       end get_con_coll;
    
     end task_2;
    

    【讨论】:

    • 谢谢。我会努力工作的。那么关联数组index by varchar2(255);
    • 不适用。您只能通过流水线函数返回嵌套表或可变数组。看看:docs.oracle.com/cd/B28359_01/appdev.111/b28425/…
    • 谢谢,非常有用。它帮助我做我想做的事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多