You are on page 1of 8

1. A package is a schema object that groups logically related PL/SQL types, variables, and subprograms.

Packages usually have two parts, a specification (spec) and a body; sometimes the body is unnecessary.
The specification is the interface to the package. It declares the types, variables, constants, exceptions,
cursors, and subprograms that can be referenced from outside the package. The body defines the
queries for the cursors and the code for the subprograms.

2. The values of the variables, constants, and cursors that a package declares (in either its specification
or body) comprise its package state. If a PL/SQL package declares at least one variable, constant, or
cursor, then the package is stateful; otherwise, it is stateless.
CREATE OR REPLACE PACKAGE BODY cursor_state IS
    PROCEDURE open_curs IS
    BEGIN
        IF NOT emp_curs%ISOPEN
        THEN OPEN emp_curs;
        END IF;
    END open_curs;

    FUNCTION f_fetch_r(n NUMBER:=1) RETURN BOOLEAN IS
        emp_first employees.first_name%TYPE;
        emp_last employees.last_name%TYPE;
        emp_sal employees.salary%TYPE;
        emp_dept departments.department_name%TYPE;
        BEGIN
            FOR count IN 1..n LOOP
                FETCH emp_curs
                INTO emp_first,emp_last,emp_sal,emp_dept;
                EXIT WHEN emp_curs%NOTFOUND;
                DBMS_OUTPUT.PUT_LINE((emp_first),(emp_last),(emp_sal),
(emp_dept));
            END LOOP;
            RETURN emp_curs%FOUND;
        END f_fetch_r;

    PROCEDURE close_curs IS
        BEGIN
            IF emp_curs%ISOPEN THEN
                CLOSE emp_curs;
            END IF;
        END close_curs;
END cursor_state;

2.A.
 DECLARE
    v_more_rows BOOLEAN:=TRUE;
 BEGIN
    cursor_state.open_curs;
    LOOP
        v_more_rows:=cursor_state.f_fetch_r(3);
        DBMS_OUTPUT.PUT_LINE('----------');
        EXIT WHEN NOT v_more_rows;
    END LOOP;
    cursor_state.close_curs;
END
2.B.

The 1, 2,3 comes from the first 3 rows and then the third call got 7 more rows, so the number resets to
1, so it repeats.
3. Oracle provides many packages with the Oracle server, either to extend the functionality of the
database or to give PL/SQL access to SQL features. You may take advantage of the functionality provided
by these packages when creating your application, or you may simply want to use these packages for
ideas in creating your own stored procedures.

3.A.
3.B.

Create, write and read files.

3.C.

INVALID_PATH

INVALID_MODE

INVALID_FILEHANDLE

INVALID_OPERATION

4. Packages are the most important construct in PL/SQL for building reusable code and plug-and-play
components, and for employing object-oriented design techniques.

You might also like