Demonstrate using a packaged ref cursor for passing sets : Package Variables « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL> create table employee
  2          (
  3           empl_no                integer         primary key
  4          ,lastname               varchar2(20)    not null
  5          ,firstname              varchar2(15)    not null
  6          ,midinit                varchar2(1)
  7          ,street                 varchar2(30)
  8          ,city                   varchar2(20)
  9          ,state                  varchar2(2)
 10          ,zip                    varchar2(5)
 11          ,zip_4                  varchar2(4)
 12          ,area_code              varchar2(3)
 13          ,phone                  varchar2(8)
 14          ,company_name           varchar2(50));

Table created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(1,'Jones','Joe','J','10 Ave','New York','NY','11111','1111','111', '111-1111','A Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(2,'Smith','Sue','J','20 Ave','New York','NY','22222','2222','222', '222-111','B Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(3,'Anderson','Peggy','J','500 St','New York','NY','33333','3333','333', '333-3333','C Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(4,'Andy','Jill', null,'930 St','New York','NY','44444','4444','212', '634-7733','D Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(5,'OK','Carl','L','19 Drive','New York','NY','55555','3234','212', '243-4243','E Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(6,'Peter','Jee','Q','38 Ave','New York','NY','66666','4598','212', '454-5443','F Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(7,'Baker','Paul','V','738 St.','Queens','NY','77777','3842','718', '664-4333','G Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(8,'Young','Steve','J','388 Ave','New York','NY','88888','3468','212', '456-4566','H Associates Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(9,'Mona','Joe','T','9300 Ave','Kansas City','MO','99999','3658','415', '456-4563','J Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(10,'Hackett','Karen','S','Kings Rd. Apt 833','Bellmore','NY','61202','3898','516', '767-5677','AA Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(11,'Bob','Jack','S','12 Giant Rd.','Newark','NJ','27377','3298','908', '123-7367','Z Associates');

1 row created.

SQL>
SQL>
SQL>
SQL>
SQL> create or replace package mytypes
  2  as
  3     type g_rc is ref cursor;
  4  end;
  5  /

Package created.

SQL> show errors
No errors.
SQL>
SQL> create or replace procedure cust_names
  2  (p_rc out mytypes.g_rc)
  3  as
  4  begin
  5    open p_rc for 'select firstname, lastname
  6                   from employee';
  7  end;
  8  /

Procedure created.

SQL> show errors
No errors.
SQL>
SQL> var temp refcursor
SQL> exec cust_names(:temp)

PL/SQL procedure successfully completed.

SQL> print temp

FIRSTNAME       LASTNAME
--------------- --------------------
Joe             Jones
Sue             Smith
Peggy           Anderson
Jill            Andy
Carl            OK
Jee             Peter
Paul            Baker
Steve           Young
Joe             Mona
Karen           Hackett
Jack            Bob

11 rows selected.

SQL>
SQL>
SQL> drop table employee;

Table dropped.

SQL>
SQL>
SQL>








27.12.Package Variables
27.12.1.Package constant variable
27.12.2.Cursor variable in a package
27.12.3.Serially Reusable Packages
27.12.4.Test unit for package scopes
27.12.5.Define constant in a package
27.12.6.Package level cursor variable
27.12.7.Use function to initialize the package level variable
27.12.8.Pre-filled table collection of varchars in a package
27.12.9.Private field
27.12.10.Use package to define variable and use across code blocks
27.12.11.Demonstrate using a packaged ref cursor for passing sets