pl-sql中函数循环插入值方式一

create or replace function F_TEST( p_type  in number, p_startdate in varchar2, p_enddate   in varchar2)
--type类型table_test
return table_test
pipelined is
  pragma autonomous_transaction;--自动事物
  --type类型row
  v_row_test row_test;
  type generic_cr is ref cursor;
  cr_sqlcur           generic_cr;--cursor类型
  v_sql              varchar2(4096);
  --声明参数
  v_orgcode           varchar2(32);
  v_orgname           varchar2(128);

--根据不同类型,组合需要的sql
if p_type = 1 then
  v_sql:='select  * from table1 where
   h.startdate > to_date('''||p_startdate||''',''yyyy-mm-dd'')-1
   and h.enddate < to_date('''||p_enddate||''',''yyyy-mm-dd'')+1';
end if;


if p_type = 2 then
  v_sql:='select  * from table2 where
   h.startdate > to_date('''||p_startdate||''',''yyyy-mm-dd'')-1
   and h.enddate < to_date('''||p_enddate||''',''yyyy-mm-dd'')+1';
end if;

--新建一个表,两个字段,将sql插入到表中,方便查看sql
  insert into test (msn) values (v_sql);
  commit;
 
  --打开循环--open loop fetch into
  open cr_sqlcur for v_sql;
  loop
    fetch cr_sqlcur
      into
               v_orgcode,
               v_orgname;

    exit when cr_sqlcur%notfound;

             --type row 整合输出的字段
             v_row_test := row_test( v_orgcode,v_orgname);
        -- 管道输出
        pipe row(v_row_test);

    end loop;--循环结束

  close cr_sqlcur;
  commit;
  return;
end F_TEST;

--open loop fetch into
将值插入到自定义字段中--然后用管道输出

猜你喜欢

转载自yunqiang-zhang-hotmail-com.iteye.com/blog/1405212