Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Parameters

argument

Actual Parameters

Formal parameter

1. In your own words, describe parameters and the purpose they serve in PL/SQL
subprograms.

Parameters are used to pass values that may vary into a subprogram. They make

procedures more flexible; a procedure can be created once and then executed
many times

with different values for the parameters.

2. Using the COUNTRIES table:

A. Create a procedure that accepts a country_id as a parameter and displays the


name of the

country and its capitol city. Name your procedure get_country_info. Save your
procedure

definition for later use.

CREATE OR REPLACE PROCEDURE get_country_info

(p_country_id IN wf_countries.country_id%TYPE)

IS

v_country_name wf_countries.country_name%TYPE;

v_capitol wf_countries.capitol%TYPE;

BEGIN
SELECT country_name, capitol

INTO v_country_name, v_capitol

FROM wf_countries

WHERE country_id = p_country_id;

DBMS_OUTPUT.PUT_LINE('The country name is '||v_country_name||' and the


capital city is '||

v_capitol);

END;

B. Execute your procedure from an anonymous block, using country_id 90.

BEGIN

 get_country_id(90);

END;

C. Re-execute the procedure from the anonymous block, this time using country_id
95. What

happens?

An ORA-01403 error is returned because country_id 95 does not exist.

D. Retrieve your procedure code from Saved SQL and modify it to trap the
NO_DATA_FOUND

exception in an exception handler. Execute the modified procedure using


country_id 95 again.

Now what happens?


CREATE OR REPLACE PROCEDURE get_country_info(p_country_id IN

wf_countries.country_id%TYPE)

IS

v_country_name wf_countries.country_name%TYPE;

v_capitol wf_countries.capitol%TYPE;

BEGIN

SELECT country_name, capitol

INTO v_country_name, v_capitol

FROM wf_countries

WHERE country_id = p_country_id;

DBMS_OUTPUT.PUT_LINE('The country name is '||v_country_name||' and the


capital city is '||

v_capitol);

EXCEPTION

WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('This country does not


exist');

END;

3. In your own words, describe what a formal parameter is and what an actual
parameter is. Also,

name three variations for an actual parameter.

A formal parameter is the parameter in the header of a procedure that gives the
parameter a name

and datatype. An actual parameter is the value passed from the calling
environment when the
procedure is executed.

The formal parameter name is referenced within the procedure code. Actual
parameter can be

expressions, literal values, or variables.

4. Procedure Exercise:

A. Write a procedure that displays the number of countries in a given region whose
highest

elevations exceed a given value. The procedure should accept two formal
parameters, one for

a region_id and the other for an elevation value for comparison. Use

DBMS_OUTPUT.PUT_LINE to display the results in a message. Save your procedure


code.

CREATE OR REPLACE PROCEDURE get_country_count

(p_region_id wf_countries.region_id%TYPE

,p_elevation wf_countries.highest_elevation%TYPE)

IS

v_count NUMBER(4);

BEGIN

SELECT COUNT(*) INTO v_count

FROM wf_countries

WHERE region_id = p_region_id

AND highest_elevation > p_elevation;

DBMS_OUTPUT.PUT_LINE ('Region '||p_region_id||' contains '||v_count||'


countries higher than '||
p_elevation);

END;

B. Execute your procedure using the value 5 for the region_id and 2000 for the
highest elevation.

BEGIN

get_country_count(5,2000);

END;

Region 5 contains 10 countries higher than 2000

C. DESCRIBE your procedure to check the names and datatypes of its formal
parameters.

DESCRIBE get_country_count

D. Retrieve your procedure code from Saved SQL and modify it to accept a third
formal parameter

of datatype CHAR. The procedure should display a count of the number of


countries in a given

region whose highest elevations exceed a given value and whose country name
starts with a

given alphabetic character. Your SELECT statement should include a WHERE


condition to

compare the first character of each country’s name with the third parameter value
(Hint: use

SUBSTR). Save your work again and DESCRIBE the modified procedure.
E. Write an anonymous block which declares three variables to store actual
parameter values for

the region_id, elevation, and area, and then executes the procedure passing these
values.

Execute the block using values 5, 2000, and ‘B’.

DECLARE

v_region_id wf_countries.region_id%TYPE := 5;

v_elevation wf_countries.highest_elevation%TYPE := 2000;

v_char CHAR(1) := 'B';

BEGIN

    get_country_count(v_region_id, v_elevation, v_char);

END;

F. Modify your anonymous block to use the same actual parameter values but pass
them to the

procedure in a different order: (5, ‘B’, 2000). Execute the block. What happens and
why?

DECLARE

v_region_id wf_countries.region_id%TYPE := 5;

v_elevation wf_countries.highest_elevation%TYPE := 2000;

v_char CHAR(1) := 'B';

BEGIN

get_country_count(v_region_id, v_char, v_elevation);

END;
An ORA-06502 data conversion error is returned because the procedure call
cannot convert an actual

parameter (‘B) of datatype CHAR to a formal parameter of datatype NUMBER.

You might also like