Co2 Session 12
Co2 Session 12
Co2 Session 12
TOPIC:
PROCEDURES IN PL/SQL
Session - 12
1
PROCEDURES IN PL/SQL
2
PL/SQL BLOCK
3
DECLARATIONS
Declaring variable of the type of a column of specified table OR a type of other variable.
4
ASSIGNMENTS
In the last statement, the variable x is set to the result of the SELECT statement.
CONTROL STATEMENTS
• PL/SQL supports the usual conditional, iterative, and sequential flow-of-control mechanisms.
• Conditional IF statement
6
ITERATION STATEMENTS
7
CURSORS IN PL/SQL
• SELECT statement can be used if the query returns one and only one row.
• To handle a query that can return an arbitrary number of rows (that is, zero, one, or more rows)
PL/SQL uses cursors to allow the rows of a query result to be accessed one at a time.
• In effect, the cursor acts as a pointer to a particular row of the query result.
• A cursor must be declared and opened before it can be used, and it must be closed to deactivate it
after it is no longer required.
• Once the cursor has been opened, the rows of the query result can be retrieved one at a time using a
FETCH statement, as opposed to a SELECT statement.
8
EXAMPLE OF CURSOR
9
PARAMETERIZED CURSORS
• Passing parameters to cursors PL/SQL allows cursors to be parameterized, so that the same cursor
definition can be reused with different criteria.
10
SUBPROGRAMS, STORED PROCEDURES, FUNCTIONS,
AND PACKAGES
•Subprograms are named PL/SQL blocks that can take parameters and be invoked.
•PL/SQL has two types of subprogram called (stored) procedures and functions.
•Procedures and functions can take a set of parameters given to them by the calling program and
perform a set of actions.
•The difference between a procedure and a function is that a function will always return a single
value to the caller, whereas a procedure does not.
•Usually, procedures are used unless only one return value is needed.
11
STORED PROCEDURES AND FUNCTIONS
•Procedures and functions are very similar to those found in most high-level programming languages and
have the same advantages:
•This reduces duplication of effort and improves software modularity and extensibility.
•aid abstraction
•In databases, many applications can access the procedure stored on the server. Server acts as a single point
of change.
•Stored procedures and functions allow business logic to be stored in databases which can be invoked from
SQL statements.
12
GENERAL FORM FOR CREATING AND CALLING
PROCEDURES AND FUNCTIONS
13
PARAMETER TYPES
•A parameter has a specified name and data type but can also be designated as:
14
EXAMPLE OF CREATE FUNCTION AND
FUNCTION CALL
15
EXAMPLE OF CREATE PROCEDURE AND CALL
PROCEDURE
query set the d_count variable of the dept_count_proc procedure with the number of instructors of Physics
department.
The procedure could also be executed in SQL*Plus as:
16
PACKAGES IN PL/SQL
• Packages (PL/SQL) A package is a collection of procedures, functions, variables, and SQL statements that are
grouped together and stored as a single program unit.
• A package has two parts: a specification and a body.
• A package’s specification declares all public constructs of the package, and the body defines all constructs
(public and private) of the package, and so implements the specification.
•Oracle database performs the following steps when a procedure or package is created:
17
EXAMPLES
•and we could create the package body (that is, the implementation of the package) as:
•To reference the items declared within a package specification, we use the dot notation. For
example, we could call the PropertiesForStaff procedure as follows:
StaffPropertiesPackage.PropertiesForStaff(‘SG14’);
18