从PL / SQL函数返回表 - 不正确的数据类型

时间:2016-12-29 15:11:41

标签: plsql

我的函数有问题应该返回一个表。但是,当我想运行此函数时,将返回错误“不正确的数据类型”。无论如何包裹是正确的。

我不知道问题出在哪里。

这是我的代码:

    create or replace package PKG_BORROW as
    type advertisement_header is record(
    id_advertisment number,
    title   varchar2(100),
    date_added timestamp,
    username varchar2(100),
    regionName varchar2(100),
    id_category number
    );
    type adv_head_tab is table of advertisement_header;



    enter code here

   function get_adv_header_filter
  (title varchar2,username varchar2,regionName varchar2,categoryName varchar2)
  return adv_head_tab;

  end ;

  /
  create or replace package body PKG_BORROW 
  as

  function get_adv_header_filter
  (title varchar2,username varchar2,regionName varchar2,categoryName varchar2)
  return adv_head_tab as
  l_tab adv_head_tab := adv_head_tab();
  query varchar2(500);
   TYPE CurTyp  IS REF CURSOR;
  v_adv_cursor    CurTyp;

  begin
    query:='select a.id_advertisement,a.title,a.date_added,a.id_user,r.name,i.id_item_category from advertisement a,region r,item_category i
    where a.id_region=r.id_region and a.id_item_category=i.id_item_category ';

    open v_adv_cursor for query;
    loop
        fetch v_adv_cursor into l_tab(l_tab.last);
        exit when v_adv_cursor%NOTFOUND;
    end loop;
   close v_adv_cursor;
   dbms_output.put_line(l_tab(1).title);
    return l_tab;
  end;

end ;

  /
  select * from TABLE(PKG_BORROW.get_adv_header_filter('a','a','a','a'));

  /
  declare
     TYPE CurTyp  IS REF CURSOR;
  v_adv_cursor    CurTyp;
  begin 
  open v_adv_cursor for PKG_BORROW.get_adv_header_filter('a','a','a','a');

  end;

更新:嗯,我使我的代码更简单,但我仍然有00902错误“无效的数据类型”。新代码我在下面显示:

     create or replace package PKG_BORROW as
    type adv_head_tab is table of ADVERTISEMENT%ROWTYPE;
   function get_adv_header_filter
  return adv_head_tab;

  end ;

  /
  create or replace package body PKG_BORROW 
  as

  function get_adv_header_filter
  return adv_head_tab as
  l_tab adv_head_tab := adv_head_tab();
  query varchar2(500);
   TYPE CurTyp  IS REF CURSOR;
  v_adv_cursor    CurTyp;

 begin
    query:='select * from ADVERTISEMENT';

    open v_adv_cursor for query;

    fetch v_adv_cursor bulk collect into l_tab;

   close v_adv_cursor;


   for rec in 1..l_tab.count
   loop        
     dbms_output.put_line(l_tab(rec).title);
   end loop;

    return l_tab;
  end;
end ;

  /
  select  * from TABLE(PKG_BORROW.get_adv_header_filter);

有关无效数据类型的错误仍然存​​在。

1 个答案:

答案 0 :(得分:0)

你的功能有问题。您正在尝试将光标值插入循环中的集合。你需要做批量收集。见下文:

 function get_adv_header_filter
  (title varchar2,username varchar2,regionName varchar2,categoryName varchar2)
  return adv_head_tab as
  l_tab adv_head_tab := adv_head_tab();
  query varchar2(500);
   TYPE CurTyp  IS REF CURSOR;
  v_adv_cursor    CurTyp;

  begin
    query:='select a.id_advertisement,a.title,a.date_added,a.id_user,r.name,i.id_item_category from advertisement a,region r,item_category i
    where a.id_region=r.id_region and a.id_item_category=i.id_item_category ';

    open v_adv_cursor for query;

    fetch v_adv_cursor bulk collect into l_tab;

   close v_adv_cursor;


   for rec in 1..l_tab.count
   loop        
     dbms_output.put_line(l_tab(rec).title);
   end loop;

    return l_tab;
  end;