SYS_REFCURSOR as parameter : Cursor function « Cursor « Oracle PL/SQL Tutorial






SQL>
SQL> create table product(
  2     product_id number(4)     not null,
  3     product_description varchar2(20) not null
  4  );

Table created.

SQL>
SQL> insert into product values (1,'Java');

1 row created.

SQL> insert into product values (2,'Oracle');

1 row created.

SQL> insert into product values (3,'C#');

1 row created.

SQL> insert into product values (4,'Javascript');

1 row created.

SQL> insert into product values (5,'Python');

1 row created.

SQL>
SQL>
SQL> create table company(
  2     product_id        number(4)    not null,
  3     company_id          NUMBER(8)    not null,
  4     company_short_name  varchar2(30) not null,
  5     company_long_name   varchar2(60)
  6  );

Table created.

SQL> insert into company values(1,1001,'A Inc.','Long Name A Inc.');

1 row created.

SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.');

1 row created.

SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.');

1 row created.

SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.');

1 row created.

SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.');

1 row created.

SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.');

1 row created.

SQL>
SQL>
SQL> create table org_company_site(
  2     company_id number(8) not null,
  3     site_no number(4) not null
  4  );

Table created.

SQL> insert into org_company_site values (1001,1);

1 row created.

SQL> insert into org_company_site values (1002,2);

1 row created.

SQL> insert into org_company_site values (1003,3);

1 row created.

SQL> insert into org_company_site values (1004,1);

1 row created.

SQL> insert into org_company_site values (1004,2);

1 row created.

SQL> insert into org_company_site values (1004,3);

1 row created.

SQL> insert into org_company_site values (1005,1);

1 row created.

SQL> insert into org_company_site values (1005,4);

1 row created.

SQL> insert into org_company_site values (1005,5);

1 row created.

SQL> insert into org_company_site values (1006,1);

1 row created.

SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION f_cursor(p_cursor SYS_REFCURSOR)
  2  RETURN NUMBER
  3  IS
  4    v_company_short_name VARCHAR2(30);
  5    v_cnt NUMBER :=0;
  6    v_ret_code NUMBER;
  7  BEGIN
  8    BEGIN
  9      LOOP
 10        FETCH p_cursor INTO v_company_short_name;
 11        EXIT WHEN p_cursor%NOTFOUND;
 12        v_cnt :=v_cnt +1;
 13      END LOOP;
 14      IF (v_cnt >0)THEN
 15        v_ret_code :=1;
 16      ELSE
 17        v_ret_code :=0;
 18      END IF;
 19    EXCEPTION WHEN OTHERS THEN
 20      v_ret_code :=SQLCODE;
 21    END;
 22    RETURN (v_ret_code);
 23  END;
 24  /

Function created.

SQL>
SQL>
SQL>
SQL> SELECT rpad(h.product_description,20,' ') "Hierarchy",
  2         rpad(o.company_short_name,30,' ') "Organization"
  3  FROM product h,company o
  4  WHERE h.product_id =o.product_id
  5  AND f_cursor(
  6        CURSOR(SELECT o1.company_short_name
  7               FROM company o1
  8               WHERE o1.company_id =o.company_id
  9               AND 1 < (SELECT count(os.site_no)
 10                        FROM org_company_site os
 11                        WHERE os.company_id =o1.company_id)
 12              )
 13  )=1;

Hierarchy            Organization
-------------------- ------------------------------
Oracle               D Inc.
Oracle               E Inc.

2 rows selected.

SQL>
SQL>
SQL> drop table company;

Table dropped.

SQL>
SQL> drop table product;

Table dropped.

SQL>
SQL> drop table org_company_site;

Table dropped.

SQL>








25.16.Cursor function
25.16.1.Cursor function
25.16.2.A PL/SQL function that uses a cursor expression
25.16.3.Cursor expressions using multiple levels of nested cursors
25.16.4.SYS_REFCURSOR as parameter
25.16.5.Passing data from one table function to another in a pipelined fashion