A package with one method
SQL>
SQL> CREATE TABLE lecturer (
2 id NUMBER(5) PRIMARY KEY,
3 first_name VARCHAR2(20),
4 last_name VARCHAR2(20),
5 major VARCHAR2(30),
6 current_credits NUMBER(3)
7 );
Table created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10001, 'Scott', 'Lawson','Computer Science', 11);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major, current_credits)
2 VALUES (10002, 'Mar', 'Wells','History', 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10003, 'Jone', 'Bliss','Computer Science', 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10004, 'Man', 'Kyte','Economics', 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10005, 'Pat', 'Poll','History', 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10006, 'Tim', 'Viper','History', 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10007, 'Barbara', 'Blues','Economics', 7);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10008, 'David', 'Large','Music', 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10009, 'Chris', 'Elegant','Nutrition', 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10010, 'Rose', 'Bond','Music', 7);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10011, 'Rita', 'Johnson','Nutrition', 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
2 VALUES (10012, 'Sharon', 'Clear','Computer Science', 3);
1 row created.
SQL>
SQL>
SQL> CREATE OR REPLACE PACKAGE PersistPkg AS
2 PRAGMA SERIALLY_REUSABLE;
3
4 TYPE t_StudentTable IS TABLE OF lecturer.ID%TYPE
5 INDEX BY BINARY_INTEGER;
6
7 v_MaxRows NUMBER := 5;
8
9 PROCEDURE Readlecturer(p_StudTable OUT t_StudentTable,
10 p_NumRows OUT NUMBER);
11 END PersistPkg;
12 /
Package created.
SQL>
SQL> CREATE OR REPLACE PACKAGE BODY PersistPkg AS
2 PRAGMA SERIALLY_REUSABLE;
3 CURSOR StudentCursor IS
4 SELECT ID
5 FROM lecturer
6 ORDER BY last_name;
7
8 PROCEDURE Readlecturer(p_StudTable OUT t_StudentTable,
9 p_NumRows OUT NUMBER) IS
10 v_Done BOOLEAN := FALSE;
11 v_NumRows NUMBER := 1;
12 BEGIN
13 IF NOT StudentCursor%ISOPEN THEN
14 OPEN StudentCursor;
15 END IF;
16
17 WHILE NOT v_Done LOOP
18 FETCH StudentCursor INTO p_StudTable(v_NumRows);
19 IF StudentCursor%NOTFOUND THEN
20 CLOSE StudentCursor;
21 v_Done := TRUE;
22 ELSE
23 v_NumRows := v_NumRows + 1;
24 IF v_NumRows > v_MaxRows THEN
25 v_Done := TRUE;
26 END IF;
27 END IF;
28 END LOOP;
29
30 p_NumRows := v_NumRows - 1;
31
32 END Readlecturer;
33 END PersistPkg;
34 /
Package body created.
SQL>
SQL> drop table lecturer;
Table dropped.
Related examples in the same category