0% found this document useful (0 votes)
3 views

CLO1 02 Procedures&Functions

The document outlines the implementation of PL/SQL procedures and functions, detailing their creation, removal, and application in database operations. It includes syntax examples, parameter types, and the advantages of using stored procedures and functions for backend processing. The document serves as a guide for understanding and utilizing PL/SQL blocks in enterprise database applications.

Uploaded by

x.tm.x23
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CLO1 02 Procedures&Functions

The document outlines the implementation of PL/SQL procedures and functions, detailing their creation, removal, and application in database operations. It includes syntax examples, parameter types, and the advantages of using stored procedures and functions for backend processing. The document serves as a guide for understanding and utilizing PL/SQL blocks in enterprise database applications.

Uploaded by

x.tm.x23
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CLO1: PL/SQL PROCEDURES AND

FUNCTIONS
CLO1: Implement stored procedures, functions, and
triggers for backend processing.
Document Revision
CONTROL (DRC)

Versio Autho Effectiv Change DRC


n r e Date Descripti No
on
1.0 Nourc Dec Defined 1.0
X: MAJOR CHANGE
hene 2022 the first
Y: MINOR CHANGE
Benay version
ed
2.0 Ghazal Dec Added 2.0
a 2022 Slides 5, 7,
Bilquis 10, 11, 22,
e 24, 26
Updated
CIA 4203 - ENTERPRISE DATABASE APPLICATIONS
Slides 8, 2
25, 27
CREATING PROCEDURES

CREATING FUNCTIONS LECTURE


OUTLINE
REMOVING
PROCEDURES AND
FUNCTIONS

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS


Objectives

 After completng this lesson, you should be able to do the following:


 Describe what are procedures and functions?
 Create stored procedures
 Create stored functions
 Remove stored procedures and functions

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 4


PL/SQL Blocks

 A PL/SQL Block may be


PL/SQL
anonymous or named blocks
 Functions and Procedures are
named PL/SQL blocks
Anonymo
 They are stored as database Named
us
objects in the workspace
 They can be reused
Procedur
Function Trigger
e

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 5


 A procedure
 Is a type of subprogram that performs
What is a an action
Procedure?  Is stored in the database as a schema
object
 Promotes reusability and maintainability

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 6


Application of PL/SQL Stored Procedure

 A Procedure is a standalone block that is mainly used for performing


tasks (DML operations)
 Example: A procedure may be used to -
 Create a new record(s)
 Update salaries of all employees in a given department
 Delete a specified project only if it is not completed

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 7


Syntax of Creating Procedure

 Use CREATE PROCEDURE followed by the


name , optional parameters, and keyword IS
or AS
 Add the OR REPLACE option to overwrite the
existing procedure
 Write a PL/SQL block containing local
variables, a BEGIN, and an END
 A procedure can modify table data. It can
execute INSERT, UPDATE and DELETE
statements within the execution section
 Use the SQL%ROWCOUNT cursor attribute
to return the number of records processed

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS


Source: 8
What are parameters?

 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

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS


Source: 9
CREATING A
PROCEDURE CREATE OR REPLACE PROCEDURE RaiseSalary
(vPercent Number)
AS

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;

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 10


CREATING A
PROCEDURE
Using the EXECUTE command (does not
work in Online Environment)

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;

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 11


11
Using IN Parameters

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 12


Using OUT Paramters

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 13


USING IN OUT Parameters

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 14


Summary of Parameter Modes

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 15


Source:
Invoking Procedures

 You can invoke paarmeters by using:


 Anonymous blocks
 Another proecure

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 16


Handled Exceptions

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 17


Source:
Handled Exceptions-Example

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 18


Source:
Exceptions Not Handled

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 19


Source:
Exceptions Not Handled-Example

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 20


Source:
Removing Procedures

Syntax Example

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 21


Source:
Advantages of a stored procedure

 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

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 22


 A function
 Is a named PL/SQL block that returns a
value
 Can be stored in the database as a
What is a schema object for repeated execution
Function?  Is called as part of an expression or is
used to provide a parameter value

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 23


Application of PL/SQL Functions

 Functions are a standalone block that is mainly used for performing


computations that return a single value.
 Example: A function may take:
 A Date as an input parameter, calculate and return the Age
 Take the Salary as an input parameter, calculate and return the Bonus
 A Project ID as input parameters and return the Total Revenue of the project
 An Employee ID and Task Status as an input parameter and return the
Number of Tasks of the given status

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 24


Syntax of Creating Function

 Use CREATE FUNCTION followed by the


name , optional parameters, and keyword
IS or AS
 Add at least one RETURN statement
 Add the OR REPLACE option to overwrite
the existing function
 Write a PL/SQL block containing local
variables, a BEGIN, and an END
 A function cannot modify table data. It
cannot execute INSERT, UPDATE and
DELETE statements.

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS


Source: 25
CREATING A
FUNCTION
CREATE OR REPLACE FUNCTION totalJobSalary
(pJobID varchar)
RETURN Number AS
vTotalSalary Number(7);
BEGIN
SELECT sum(salary)
 Create a function called INTO vTotalSalary
totalJobSalary that FROM HR_EMPLOYEES
returns the total salary WHERE job_id = pJobID;
of a given JOB
 Test the function RETURN vTotalSalary;

END;

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 26


Using the Dual Table
CREATING A
FUNCTION SELECT totalJobSalary(‘IT_PROG’) FROM dual;

Using a PL/SQL block

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;

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 27


Stored Function: Example- Invoke a function

 Invoke the function


 Invoke the function as
 As a parameter to another subprogram
 A part of a PL/SQL expression using a host
variable

 In an SQL statement

 A part of a PL/SQL expression using a local


variable

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS


Source: 29
Removing Functions

Syntax Example

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 30


Procedures Vs. Functions

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 31


Key Terms

Stored Stored
Procedures Functions

CIA 4203 - ENTERPRISE DATABASE APPLICATIONS 32


800 MyHCT (800 www.hct.ac.ae
69428)

You might also like