CLO1 02 Procedures&Functions
CLO1 02 Procedures&Functions
FUNCTIONS
CLO1: Implement stored procedures, functions, and
triggers for backend processing.
Document Revision
CONTROL (DRC)
Parameters:
Are declared after the subprogram name in
the PL/SQL header
Pass or communicate data between the
caller and the subprogram
Can be
IN parameter (the default) provides values for a
subprogram to process
OUT parameter returns a value to the caller
IN OUT parameter supplies an input value,
which may be returned (output) as a modified
value
BEGIN
UPDATE HR_EMPLOYEES
SET salary = salary + salary * vPercent;
Create a procedure
called RaiseSalary that dmbs_ouptput.put_line(‘Salaries Updated’);
increases all employees EXCEPTION
salary by a given When OTHERS Then
percentage dmbs_ouptput.put_line(‘An error has
Test the procedure occured’);
END;
EXECUTE RaiseSalary(0.15);
Create a procedure
called RaiseSalary that
increases all employees
salary by a given Using a PL/SQL block
percentage BEGIN
Test the procedure RaiseSalary(0.15);
END;
Syntax Example
Performance
Multiple SQL statements may be grouped in a stored procedure and executed
in a single call
Reusability
A procedure may be reused in a number of applications
Security
Data access may be restricted by allowing users to manipulate data only
through stored procedures without providing access to the underlying tables
END;
DECLARE
vresult Number(7);
Create a function called BEGIN
vresult := totalJobSalary(‘IT_PROG’);
totalJobSalary that
dbms_output.put_line (vresult);
returns the total salary
END;
of a given JOB
Test the function
Using the function in a SQL query to get the total
salaries of all Jobs
SELECT job_id, job_title, totalJobSalary(job_id)
FROM HR_JOBS;
In an SQL statement
Syntax Example
Stored Stored
Procedures Functions