Calling a Cursor Declared in a Different Package : Packages « Function Procedure Packages « Oracle PL/SQL Tutorial






-- create demo table
create table Employee(
  ID                 VARCHAR2(4 BYTE)         NOT NULL,
  First_Name         VARCHAR2(10 BYTE),
  Last_Name          VARCHAR2(10 BYTE),
  Start_Date         DATE,
  End_Date           DATE,
  Salary             Number(8,2),
  City               VARCHAR2(10 BYTE),
  Description        VARCHAR2(15 BYTE)
)
/

-- prepare data
insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
             values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',  'Programmer')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
              values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
              values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
              values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
              values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
              values('06','Linda',    'Green',   to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York',  'Tester')
/
insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
              values('07','David',    'Larry',   to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York',  'Manager')
/

insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
              values('08','James',    'Cat',     to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')
/

-- display data in the table
select * from Employee
/

--Here is the package spec where the cursor is declared.

create or replace package pkg_Util is
    cursor c_emp is select * from employee;
    r_emp c_emp%ROWTYPE;
end;
/

ge created.

--Here is a different package that references the cursor

create or replace package body pkg_aDifferentUtil is
    procedure p_printEmps is
    begin
        open pkg_Util.c_emp;
        loop
            fetch pkg_Util.c_emp into pkg_Util.r_emp;
            exit when pkg_Util.c_emp%NOTFOUND;
            DBMS_OUTPUT.put_line(pkg_Util.r_emp.first_Name);
        end loop;
        close pkg_Util.c_emp;
     end;
end;
/

 -- clean the table
drop table Employee
/SQL>








27.10.Packages
27.10.1.Packages
27.10.2.Private Versus Public Package Objects
27.10.3.Package State
27.10.4.Recompiling Packages
27.10.5.All packages can be recompiled by using the Oracle utility dbms_utility:
27.10.6.Creating a Package Specification
27.10.7.Creating a Package Body
27.10.8.Creating Packages and call its functions
27.10.9.Calling Functions and Procedures in a Package
27.10.10.A Package Specification and its body
27.10.11.Overloading Packaged Subprograms
27.10.12.Calls procedure in a package
27.10.13.Dropping a Package
27.10.14.Calling a Cursor Declared in a Different Package
27.10.15.Reference fields and methods in package
27.10.16.Controlling access to packages
27.10.17.Globals Stored in a Package
27.10.18.A Subtypes Example
27.10.19.Generate Random number
27.10.20.Crosss reference between two packages
27.10.21.package RECURSION
27.10.22.Using RESTRICT_REFERENCES in a Package
27.10.23.PLS-00452: Subprogram 'GETNAME' violates its associated pragma
27.10.24.Dynamically create packages