Professional Documents
Culture Documents
Learning Objectives
After studying this lesson, you should be able to:
Subprograms
Subprograms are named blocks which can be compiled and stored in
the database. It promotes modularized program that are extensible,
reusable and maintainable. These subprograms can either be
procedures or functions.
Procedures
Syntax
2
MODULE OF INSTRUCTION
Example 1
Parameters are used to transfer data values to and from the calling
environment. They are treated the sme way like local variables. The
mode option defines whether IN (default), OUT, IN OUT parameters
are used.
Example 2
Example 3
4
MODULE OF INSTRUCTION
The value 100 of v_id is passed to p_id of the getsalary procedure. The
SELECT statement uses this value to retrieve the salary of the
employee, stores it to the out parameter p_sal and returns the value to
v_sal. The value of v_sal is then used to update the salary of the
employees.
Example 4
Example 5
Functions
Syntax
Do not include
size specificationn
in the RETURN
data type .
The RETURN data type must not include a size specification. There
must be at least one RETURN expression statement.
Example 6
6
MODULE OF INSTRUCTION
Each row of the salary column is passed to the function. If the salary is
higher than 20,000, then the function returns salary grade ‘A’. If the
the salary is lower than 20,001 but higher than 10,000, the function
returns ‘B’. Otherwise it returns salary grade ‘C’.
Result
Example 7
Triggers
Triggers are similar to stored procedures. However, they differ in the
way that they are invoked. A procedure is explicitly run by a user,
application, or a trigger. Whereas triggers are implicitly
(automatically) fired by the database when a triggering event occurs.
Syntax
• Security
• Auditing
• Data integrity
• Referential integrity
• Table replication
• Computing derived data automatically
• Event logging
8
MODULE OF INSTRUCTION
Example 8
Glossary
Function
- A type of subprogram that returns one value and has only input
parameters.
Procedure
Subprograms
Trigger
References
Hoffer, J., Ramesh, V., Topi, H. (2013). Modern Database
Management, 11th Ed. New Jersey: Pearson Education, Inc.
Price, J. (2008). Oracle Database 11g SQL: Master SQL and PL/QL in
the Oracle Database. New York: McGraw-Hill.
10