You are on page 1of 10

MODULE OF INSTRUCTION

Subprograms and Triggers

The previous module revealed the real power of relational model by


examining multiple table queries in some detail. This module provides
an understanding of the importance of SQL in developing an
application.

Subprograms are named PL/SQL blocks that aid application


development by isolating operations. They are used in creating
modularized code and maintainable applications. Like subprograms,
triggers are also stored in the database and fired automatically when
specified event occur.

The syntaxes for creating subprograms differs from one vendor to


another. Thus in this module, we will be using Oracle’s PL/SQL
syntax.

Learning Objectives
After studying this lesson, you should be able to:

• Concisely define key terms


• Understand the use of SQL in procedural languages
• Understand common uses of subroprograms and database triggers
• Differentiate between a procedure and a function
• Identify the available parameter-passing modes
• Create, call and remove subprograms
• Create and remove database triggers

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.

Advanced Database Systems 1


2.0 Subprograms and Triggers

Table 2.1 Difference between Procedures and Functions

Procedures

A procedure is used to perform an action that can be called with a set


of parameters (input, output, and input output parameters).

Syntax

The REPLACE option indicates that if the procedure exists, it is


dropped and replaced with the new version created by the statement.

Table 2.2 Emp

2
MODULE OF INSTRUCTION

Example 1

The procedure update_empsalary increases the salary of the employees


by 5% if employee’s salary is less than 10,000. Otherwise an increase
of 100 for each employees with salary equal to 10,000 or higher.

Invoking the update_empsalary procedure using an anonymous block


will give you the following results:

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.

• IN parameter provides values for a subprogram to process


• OUT parameter returns a value to the calling environment
• IN OUT parameter supplies an input value, which may be
returned (output) as a modified value

Advanced Database Systems 3


2.0 Subprograms and Triggers

Table 2.3 Comparing the Parameter Modes

Example 2

Specify the Example 2 shows the modified version of Example 1 by passing


parameter data parameter value (IN mode) to the procedure update_empsalary.
type without any
precision.

The actual parameter 10,000 is being passed to the formal parameter


p_sal of the update_empsalary procedure. The value is then used in the
WHERE condition of the update statement. The results are the same.

Example 3

4
MODULE OF INSTRUCTION

The getsalary procedure consists of two parameters: p_id as an IN


parameter mode and p_sal as an OUT parameter mode. The
anonymous block in the succeeding statement invoked getsalary
procedure to process the parameters.

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 4 simply creates a procedure named format_name that


changes the letter case of the employee name from the original one
(first letter is capitalized) to uppercase letters. The p_name is
processed both as an input and output parameter.

When we execute the preceding anonymous block, the SELECT


statement retrieves the name of the employee with ID 100 (that is,
Steven King) into v_name. The parameter p_name accepts the value
and returns the modified value to v_name. In this case, the employee
name is now STEVEN KING. The SERVEROUTPUT must be set to

Advanced Database Systems 5


2.0 Subprograms and Triggers

ON whenever you use the DBMS_OUTPUT package to display


results.

Example 5

The DROP command is used to remove the procedure from your


schema.

Functions

A function is similar to procedure except it returns a value. It has only


input parameters that is used to compute and return a value. A function
may be called as part of SQL expression or as part of PL/SQL
expression.

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

The function get_salgrade retrieves the salary grade of each employee.


It is used as part of the SQL SELECT statement as shown below.

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

To remove a function, use the DROP command as shown in 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.

Advanced Database Systems 7


2.0 Subprograms and Triggers

Syntax

Timing indicates when the trigger fires in relation to the triggering


event. Values are:

• BEFORE executes the trigger first before the triggering event


on a table
• AFTER fires after the triggering event on a table
• INSTEAD OF are used for views that are not otherwise
modifiable.

Trigger event types determine which DML statement causes the


trigger to fire. The possible values are INSERT, UPDATE [of
column], and DELETE. FOR EACH ROW designates that the trigger
is a row trigger. A WHEN clause applies a conditional predicate
evaluated for each row to determine whether or not to execute the
trigger body.

Common uses of triggers are for:

• Security
• Auditing
• Data integrity
• Referential integrity
• Table replication
• Computing derived data automatically
• Event logging

8
MODULE OF INSTRUCTION

Example 8

In the example, the secure_emp trigger prevents DML operations from


succeeding if the business hours (8:00am to 6:00pm Monday through
Friday) is violated. If a user attempts to make changes to the
EMPLOYEES table on Sunday, then the user sees an error message.

The RAISE_APPLICATION_ERROR is a predefined procedure that


returns an error message to the user.

Glossary
Function

- A type of subprogram that returns one value and has only input
parameters.

Procedure

- A type of subprogram that performs an action and promotes


reusability and maintainability.

Subprograms

- Are named PL/SQL statements that can be called with a set of


parameters and are stored in the database.

Trigger

- A named set of SQL statements that is executed when a


triggering DML operation occurs.

Advanced Database Systems 9


2.0 Subprograms and Triggers

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

You might also like