Co2 Session 12

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Department of CSE

COURSE NAME: DBMS


COURSE CODE:22CS2110 RA

TOPIC:
PROCEDURES IN PL/SQL

Session - 12

1
PROCEDURES IN PL/SQL

• PL/SQL has concepts similar to modern programming languages, such as


variable and constant declarations, control structures, exception handling,
and modularization.
• PL/SQL is a block-structured language: blocks can be entirely separate or
nested within one another. The basic units that constitute a PL/SQL
program are procedures, functions, and anonymous (unnamed) blocks.

2
PL/SQL BLOCK

• PL/SQL block has up to three parts:


• • an optional declaration part, in which variables, constants, cursors, and exceptions are
defined and possibly initialized.

• • a mandatory executable part, in which the variables are manipulated.

• • an optional exception part, to handle any exceptions raised during execution.

3
DECLARATIONS

%TYPE and %ROWTYPE attributes:

Declaring variable of the type of a column of specified table OR a type of other variable.

declaring a variable to be of the same type as a entire row of a table or view.

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

Conditional CASE statement

6
ITERATION STATEMENTS

WHILE and REPEAT


(LOOP)

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.

• The cursor can be advanced by 1 to access the next row.

• 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.

•Both can modify and return data passed to them as a parameter.

•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.

•promote reusability and maintainability

•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

Read each Create as Create or Replace

13
PARAMETER TYPES

•A parameter has a specified name and data type but can also be designated as:

•• IN – parameter is used as an input value only.

•• OUT – parameter is used as an output value only.

•• IN OUT – parameter is used as both an input and an output value.

14
EXAMPLE OF CREATE FUNCTION AND
FUNCTION CALL

This query returns all instructors of the 'Finance' department.

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:

•• It compiles the procedure or package.

•• It stores the compiled code in memory.

•• It stores the procedure or package in the database.

17
EXAMPLES

•CREATE OR REPLACE PACKAGE StaffPropertiesPackage AS procedure PropertiesForStaff(vStaffNo


VARCHAR2); END StaffPropertiesPackage;

•and we could create the package body (that is, the implementation of the package) as:

•CREATE OR REPLACE PACKAGE BODY StaffPropertiesPackage AS . . . END StaffPropertiesPackage;

•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

You might also like