Documente Academic
Documente Profesional
Documente Cultură
com
Try It / Solve It
1. In your own words, describe parameters and the purpose they serve in PL/SQL subprograms.
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.
C. Re-execute the procedure from the anonymous block, this time using country_id 95. What
happens?
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2
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?
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.
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.
B. Execute your procedure using the value 5 for the region_id and 2000 for the highest elevation.
C. DESCRIBE your procedure to check the names and datatypes of its formal parameters.
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’.
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?
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
academy.oracle.com
Try It / Solve It
1. Name the characteristics of a stored function.
2. Create a function called full_name. Pass two parameters to the function, an employee’s last name
and first name. The function should return the full name in the format, last name, comma, space,
first name (for example: Smith, Joe). Save your code.
A. Test your function from an anonymous block which uses a local variable to store and display
the returned value.
B. Modify your anonymous block from the previous step to remove the local variable declaration
and call the function directly from within the DBMS_OUTPUT.PUT_LINE call. Test the block
again.
C. Now call the function from within a SELECT statement, not a PL/SQL block. Your SELECT
statement should display the first_name, last_name, and full name (using the function) of all
employees in department 50. Your output should look like this:
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2
3. Create a function called divide that accepts two numbers as input and returns the result of
dividing the first number by the second number, rounded to two decimal places. Save your
code.
A. Test your function twice from an anonymous block using input values (50, 2)
and (25, 3).
B. Test your function a third time using input values (16, 0). What happens?
C. Modify the function code to trap the ZERO_DIVIDE exception. The exception handler should
return a value of zero from the function if ZERO_DIVIDE is raised.
D. Test your function again using input values (16,0) as before. Now what happens?
5. Look at the following two subprograms. The first is a procedure; the second is a function. Answer
the following questions.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
3
A. For a given country id, will both of these subprograms return the same results?
B. What is the advantage of creating this subprogram as a function rather than as a procedure?
C. Which of the following procedure and function calls are valid and which are not? Explain why
the invalid ones will fail.
DECLARE
v_country_id countries.country_id%TYPE := 2;
v_country_name countries.country_name%TYPE;
BEGIN
get_country_name_proc(v_country_id, v_country_name); -- Call 1
v_country_name := get_country_name_func(v_country_id); -- Call 2
v_country_name := get_country_name_proc(v_country_id); -- Call 3
END;
7. Create a function which accepts a character string as input and returns the same character string
but with the order of the letters reversed. For example, "Smith" would be returned as "htimS." Save
your code. Hint: you will need to declare a local variable to store the reversed string, and build its
contents by reading the input one character at a time (using SUBSTR) in a loop structure, starting
from the last character. Each execution of the loop reads the preceding character and
concatenates it to the reversed string.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
4
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.