A PL/SQL function that uses a cursor expression : Cursor function « Cursor « Oracle PL/SQL Tutorial






SQL>
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 or replace function f_cursor_exp return NUMBER
  2  is
  3    TYPE rc is REF CURSOR;
  4
  5    CURSOR myCursor IS
  6      SELECT h.product_description,
  7        CURSOR(SELECT o.company_long_name
  8               FROM company o
  9               WHERE o.product_id =h.product_id)long_name
 10      FROM product h;
 11
 12    productRecord rc;
 13    v_product_description VARCHAR2(20);
 14    v_company_long_name VARCHAR2(60);
 15  BEGIN
 16
 17    OPEN myCursor;
 18    LOOP
 19      FETCH myCursor INTO v_product_description,productRecord;
 20      EXIT WHEN myCursor%notfound;
 21
 22      LOOP
 23        FETCH productRecord INTO v_company_long_name;
 24        EXIT WHEN productRecord%notfound;
 25        DBMS_OUTPUT.PUT_LINE(v_product_description ||''||v_company_long_name);
 26      END LOOP;
 27    END LOOP;
 28
 29    close myCursor;
 30    RETURN (0);
 31  EXCEPTION WHEN OTHERS THEN
 32    RETURN (SQLCODE);
 33  END;
 34  /

Function created.

SQL>
SQL>
SQL>
SQL> drop table company;

Table dropped.

SQL>
SQL> drop table product;

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