PL/SQL Stored Procedures
Oracle Tips by Burleson
A well-designed stored procedure is written to achieve one purpose
and perform only the actions necessary to achieve that purpose.
Procedures can accept and return values to their calling application
with ease through the use of parameters. Table 1.1 shows stored
procedure parameter types and functionality.
The three types of procedure and function parameters.
parameters pass a value to the procedure. This value cannot be
modified by the procedure.
parameters are used to pass a value back to a calling block of
PL/SQL code. The value in this parameter can never be read by the
OUT parameters are used to pass a value to the procedure. This
value can then be modified by the procedure and the resulting
value passed back to the calling PL/SQL block.
Listing 1.9 presents a typical stored procedure. This procedure
accepts a social security number as a parameter, queries the
ENROLLED_COURSES table to determine the total number of credits
and credit hours for the student, calculates the student’s grade point
average by dividing the total number of credits by the total number of
credit hours, and updates the student’s master record in the
Listing 1.9 A typical stored procedure.
PROCEDURE Calculate_GPA (nSSN IN integer)
nOverallGPA number := 0;
iSumCredits integer := 0;
iTotalHours integer := 0;
iLogicStep integer := 0;
SELECT sum (credit_hours),
sum (decode (course_grade,
'D', 1, 0))
WHERE ssn = nSSN;
iLogicStep := 1;
nOverAllGPA := iSumCredits / iTotalHours;
iLogicStep := 2;
SET overall_gpa = nOverAllGPA
WHERE ssn = nSSN;
WHEN OTHERS THEN
SYSTEM_LOG.Log_Error (obj_name => 'Calculate_GPA',
obj_step => iLogicStep,
ora_error => substr (SQLERRM, 1, 65));
This is an excerpt from the book "High
Performance Oracle Database Automation" by
Jonathan Ingram and Donald K. Burleson, Series Editor.