Defining cursors in the package specification : Cursor Declaration « Cursor « 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>








25.2.Cursor Declaration
25.2.1.Declare cursor
25.2.2.VARRAY of Cursor
25.2.3.Select column value into a cursor variable
25.2.4.Use cursor variables
25.2.5.Use the cursor subquery
25.2.6.Returning more than one piece of information: Listing the variables separately
25.2.7.Defining a Record Type for a Cursor by Using %ROWTYPE
25.2.8.Declaring a Cursor within a Procedure
25.2.9.Defining a Cursor in an Anonymous PL/SQL Block
25.2.10.Defining cursors in the package body
25.2.11.Defining cursors in the package specification
25.2.12.Using SELECT * in a Cursor
25.2.13.Cursor for object table
25.2.14.Cursor with order by
25.2.15.Cursor rowid
25.2.16.Cursor for aggregate function