UNIT -4
PL/SQL
PROGRAMMING
PL/SQL PROGRAMMING
PL/SQL stands for "Procedural Language
/Structure Query Language”.
PL/SQL is a block structured language.
The programs of PL/SQL are logical blocks that
can contain any number of nested sub-blocks.
PL/SQL includes procedural language elements
like conditions and loops.
It allows declaration of constants and
variables, procedures and functions, types and
variable of those types and triggers. It can
support Array and handle exceptions (runtime
errors).
ADVANTAGES OF PL/SQL
1. High-Level of Execution: PL/SQL executes the entire
block of code containing multiple statements. The block
itself is an executable statement, as all the statements run
once the block executes.
2. Increased Performance: It executes the entire block of
code in one go instead of sending the statements to
the database one by one. This allows fast processing,
reducing traffic between the database and applications,
and is beneficial when dealing with large amounts of data.
3. Compatibility with SQL: PL/SQL aims to combine
procedural programming language and database language.
It extends the capabilities of SQL by adding procedural
features like loops, conditions, and data handling. It uses
SQL statements, data types, functions, cursors, data
manipulation, etc., in PL/SQL blocks.
4. Supports Portability :You can run PL/SQL applications on
any operating system and platform where Oracle Database
runs.
5. Error Management: PL/SQL performs
exception handling (runtime errors) efficiently.
6. Reusability of Code: The PL/SQL packages
enable programmers to create modular,
reusable code, enabling them to write code
once and use it across various applications.
This makes it simpler to maintain and debug
the code and reduces the amount of code
writing that must be done over and over again.
7. Security: PL/SQL provides high security level.
8. Support for developing web applications
and pages: PL/SQL supports developing web
applications and pages.
PL/SQL BLOCK
In PL/SQL, the code is not executed in single
line format, but it is always executed by
grouping the code into a single element
called Blocks.
Blocks contain both PL/SQL as well as SQL
instruction.
All these instruction will be executed as a
whole rather than executing a single
instruction at a time.
Block Structure
PL/SQL blocks have a pre-defined structure in
which the code is to be grouped. Below are
different sections of PL/SQL blocks.
1. Declaration section
2. Execution section
3. Exception-Handling section
1. DECLARATION SECTION
This is the first section of the PL/SQL blocks.
This is the section in which the declaration of
variables, cursors, exceptions, subprograms
that are needed in the block will be declared.
This particular section is optional and can be
skipped if no declarations are needed.
This section starts with the keyword
'DECLARE' .
2. EXECUTION SECTION
Execution part is the main and mandatory part
which actually executes the code that is written
inside it.
Since the PL/SQL expects the executable
statements from this block this cannot be an
empty block, i.e., it should have at least one valid
executable code line in it.
This can contain both PL/SQL code and SQL code.
This can contain one or many blocks inside it as
a nested block.
This section starts with the keyword 'BEGIN'.
This section should be followed either by 'END' or
Exception-Handling section (if present)
3. EXCEPTION-HANDLING
SECTION
The exception is unavoidable in the program which occurs
at run-time and to handle this Oracle has provided an
Exception-handling section in blocks.
This is an optional section of the PL/SQL blocks.
This is the section where the exception raised in the
execution block is handled.
This section is the last part of the PL/SQL block.
Control from this section can never return to the execution
block.
This section starts with the keyword 'EXCEPTION'.
This section should always be followed by the keyword
'END'.
The Keyword 'END' marks the end of PL/SQL block.
PL/SQL VARIABLES
A variable is a reserved memory area for storing
the data of a particular datatype.
This memory is reserved at the time of
declaration of a variable which is done in
the DECLARE section of any PL/SQL block.
Syntax for declaration of a variable:
Variable_name datatype(size);
Eg. roll_no NUMBER(2);
And if we want to assign some value to the
variable at the time of declaration itself, the
syntax would be,
Variable_name datatype(size) :=value;
Eid NUMBER(2) := 5;
PL/SQL CONSTANTS
Constants are those values which when declared
remain fixed throughout the PL/SQL block.
For declaring constants, a constant keyword is
used.
Syntax for declaring constants:
Constant_Name constant Datatype(size) :=
value;
Eg. school_name constant VARCHAR2(20) :=
“AVM";