ORACLE函数Function返回数据集合

Oracle中的Function可以返回自定义的数据集,记录参考如下:

1,Object对象

/*自定义类型 OBJECT Type*/

CREATE OR REPLACE TYPE EMP_ID_TYPE AS OBJECT(org_cd varchar2(10));

2,Table对象

/*自定义类型 TABLE Type*/

CREATE OR REPLACE TYPE EMP_ID_TABLE  AS TABLE of EMP_ID_TYPE;

3,编写Function

CREATE OR REPLACE FUNCTION F_EMP_LIST ()

 

RETURN EMP_ID_TABLE PIPELINED IS

 

 CURSOR emp_list_cursor is

            select  '20001' as emp_id from dual union 

            select  '20002' as emp_id from dual union 

            select  '20003' as emp_id from dual;

 

    v_emp_id_type EMP_ID_TYPE;   --Object对象

    v_emp_id varchar2(5);              --临时变量

 

BEGIN

 

     OPEN emp_list_cursor;

          loop

              

              fetch emp_list_cursor into v_emp_id;

              exit when emp_list_cursor%notfound;

              

              v_emp_id_type := EMP_ID_TYPE(v_emp_id);  --取值

              PIPE ROW(v_emp_id_type);   --管道

              

          end loop;

      CLOSE emp_list_cursor;

 

      return;

 

END;

 

3,测试SQL

select * from table(F_EMP_LIST);

 

猜你喜欢

转载自zhuhuide2004.iteye.com/blog/2235594