Cursor variable in a package : Package Variables « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL> create table company_site(
  2     site_no number(4)       not null,
  3     site_descr varchar2(20) not null
  4  );

Table created.

SQL> insert into company_site values (1,'New York');

1 row created.

SQL> insert into company_site values (2,'Washington');

1 row created.

SQL> insert into company_site values (3,'Chicago');

1 row created.

SQL> insert into company_site values (4,'Dallas');

1 row created.

SQL> insert into company_site values (5,'San Francisco');

1 row created.

SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE PACKAGE myPackage
  2  IS
  3    PRAGMA SERIALLY_REUSABLE;
  4    CURSOR cursor_site IS
  5      SELECT * from company_site ORDER BY site_no;
  6    PROCEDURE displaySites;
  7  END myPackage;
  8  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY myPackage
  2  IS
  3    PRAGMA SERIALLY_REUSABLE;
  4    PROCEDURE displaySites
  5    IS
  6      site_rec company_site%ROWTYPE;
  7    BEGIN
  8      OPEN cursor_site;
  9      FETCH cursor_site INTO site_rec;
 10      dbms_output.put_line(TO_CHAR(site_rec.site_no)||' '||site_rec.site_descr);
 11      FETCH cursor_site INTO site_rec;
 12      dbms_output.put_line(TO_CHAR(site_rec.site_no)||' '||site_rec.site_descr);
 13    END displaySites;
 14  END myPackage;
 15  /

Package body created.

SQL> BEGIN
  2    myPackage.displaySites;
  3  END;
  4  /
1 New York
2 Washington

PL/SQL procedure successfully completed.

SQL>
SQL> drop table company_site;

Table dropped.








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