You are on page 1of 4

For which reason might you create a subprogram within a procedure?

to bypass a pragma restriction


to bypass the CURRENT AUTHID restriction
to allow execution within a SQL statement
to store a repeating block of code once without creating a separate construct

Explanation:
Subprograms allow you to create just one occurrence of a piece of code that must be
executed in different locations of a procedure.

Use subprograms when the code is only executed within the procedure. If the code is
executed from outside the procedure, then the subprogram should be written as a packaged
or stand-alone procedure instead.

Example: (calc_comm is the subprogram)

CREATE OR REPLACE PROCEDURE update_employee


(v_emp_id IN NUMBER)
IS
v_comm NUMBER;

PROCEDURE calc_comm
IS
v_total NUMBER;
BEGIN
SELECT SUM(ord.total)
INTO v_total
FROM ord,customer
WHERE ord.custid = customer.custid
AND customer.repid = v_emp_id;
v_comm := v_total * .20;
END calc_comm;

BEGIN
...
calc_comm;
...
calc_comm;
...
calc_comm;
END;

Examine this procedure:

CREATE OR REPLACE PROCEDURE find_seats_sold


(v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN NUMBER)
IS
v_seats_sold gross_receipt.seats_sold%TYPE;
BEGIN
SELECT seats_sold
INTO v_seats_sold
FROM gross_receipt
WHERE movie_id = v_movie_id
AND theater_id = v_theater_id;
END;

Which command will successfully invoke this procedure in SQL*Plus?

EXECUTE find_seats_sold;
RUN find_seats_sold (v_theater_id => 500, v_movie_id => 34);
EXECUTE find_seats_sold (v_theater_id => 500, v_movie_id => 34);
EXECUTE find_seats_sold (v_theater_id := 500, v_movie_id := 34);

Explanation:
You can specify argument values using the positional or named method. The named
method requires the use of the "=>" operator to specify a value for each argument.

The named method:


EXECUTE find_seats_sold (v_theater_id => 500, v_movie_id => 34);

The positional method:


EXECUTE find_seats_sold (500, 34);

The value of 500 is assigned to the first argument listed in the procedure header and 34 is
assigned to the second argument.

The UPDATE_EMPLOYEE procedure contains an algorithm that calculates an


employee's commission multiple times throughout the program. If a change is
made to the algorithm, the change must be made multiple times. How can this
procedure be modified to simplify the code and reduce duplicated code?

Add an algorithm exception handler.


Create a library containing the algorithm.
Add a local subprogram containing the algorithm.
Create multiple anonymous blocks containing the algorithm.

Explanation:
Subprograms allow you to create just one occurrence of a piece of code that must be
executed in different locations of a procedure.

Use local subprograms when the code is only executed within the procedure. If the code will
be executed from outside the procedure, then the subprogram should be written as a
packaged or stand-alone procedure instead.

Example: (calc_comm is the subprogram)

CREATE OR REPLACE PROCEDURE update_employee


(v_emp_id IN NUMBER)
IS
v_comm NUMBER;

PROCEDURE calc_comm
IS
v_total NUMBER;
BEGIN
SELECT SUM(ord.total)
INTO v_total
FROM ord,customer
WHERE ord.custid = customer.custid
AND customer.repid = v_emp_id;
v_comm := v_total * .20;
END calc_comm;

BEGIN
...
calc_comm;
...
calc_comm;
...
calc_comm;
END;

Which type of construct should you create to solely perform an action without
returning a value?

view
function
procedure
packaged function

Explanation:
Procedures are usually created to perform an action without returning a value. Procedures
can return a value using an OUT argument. Functions must return a value.

Examine this procedure:

CREATE OR REPLACE PROCEDURE find_seats_sold


(v_movie_id IN NUMBER)
IS
v_seats_sold gross_receipt.seats_sold%TYPE;
BEGIN
SELECT seats_sold
INTO v_seats_sold
FROM gross_receipt
WHERE movie_id = v_movie_id;
END;

Which command will successfully invoke this procedure in SQL*Plus?

RUN find_seats_sold(34);
EXECUTE find_seats_sold;
EXECUTE find_seats_sold (34);
find_seats_sold ('Riverplace Theater');

Explanation:
Executing a procedure in SQL*Plus requires the EXECUTE command. This procedure has
one IN argument. Therefore, use this command:

EXECUTE find_seats_sold (34);

When invoking a procedure, you can specify the arguments using the positional
method by listing the values in the order of the argument list. Which method
would you use to list values in an arbitrary order?

FIFO
list
type
named
Explanation:
You can specify argument values using the positional or named method. The named
method requires the use of the "=>" operator to specify a value for each argument and
allows for an arbitrary assignment of values.

The named method:


EXECUTE find_seats_sold (v_theater_id => 500, v_movie_id => 34);

The positional method:


EXECUTE find_seats_sold (500, 34);

The value of 500 is assigned to the first argument listed in the procedure header and 34 is
assigned to the second argument.

You might also like