PL SQL
PL SQL
INTRODUCTION
PL/SQL stands for Procedural Language/Structured
Query Language. PL/SQL is an extension of the SQL
language. We can say it is a dialect or the superset of the
Structured Query Language specialized for use in the
Oracle database. Because it is Procedural language, it
eliminates many restrictions of the SQL Language. With
the use of SQL user can only manipulate the information
stored into database. User can perform very basic
operations such as selecting the information from some
prefabricated tables, inserting information into those
tables, updating the information stored in table and also
occasionally used to delete information from these tables.
PL/SQL extends SQL by adding control structures found
in the other procedural languages. Procedural constructs
blend seamlessly with Oracle SQL, resulting in a
structured, powerful language. PL/SQL combines the
SQL’s language’s ease of data manipulation and the
procedural language’s ease of programming.
SQL PL/SQL
SQL does not have any procedural ORACLE has provided all procedural
capabilities. By procedural capabilities in PL/SQL to support data
capabilities here we mean that there filtration.
is no provision of conditional
checking, looping and branching,
which are very essential for filtration
of data before entering it into
database.
SQL statements are passed to oracle In PL/SQL it sends the bundle of SQL
engine (server) one at a time. statements to the oracle server in the form
Therefore each time for each of BLOCK and hence calling the server
resources only once for that block even if
statement a call is made to the
that block is having more than one SQL
server resources and these resources statement. After processing all the
are opened and closed every time. statements in a block ORACLE server closes
And hence generating network traffic the resources results in faster execution of
resulting in slow processing. SQL statements in a PL/SQL block.
SQL PL/SQL
In SQL there is no provision of Whereas in PL/SQL, we can
handling errors and exceptions, program the block of statements to
which means that if any SQL handle the errors in such a way that
statement fails to execute, then if any of the statement fails to
oracle gives its own error execute then we can display user-
messages and error code which friendly appropriate messages.
may not be user friendly.
SQL does not support PL/SQL PL/SQL supports SQL statements
statements in its block.
PL/SQL Engine
A PL/SQL
Block
Procedural
A PL/SQL
Statement
Block Procedural Executor
S
Q
L
SQL Statement
executor
Structures of PL/SQL
Language
Declare
-Used to declare variables and constants
-Is an optional section
-Is also used to declare type declarations, PL/SQL procedures and
functions, which are local to module
Begin
-Is the executable section containing the code, which is
executed when block is run
-Is compulsory
Exception
-Handles exceptions occurring during processing.
-Used to place predefined Error-handlers or user defined
exceptions.
-Code contained in this section is executed only when an error
occurs.
-Is an optional section
End;
Declare
-Declare variables and constants
Begin
-Process SQL statements
Exception
-Error handlers
End;
PL/SQL Languages
Elements
∀♦ Operators, indicators and
punctuation
∀♦ Identifiers
∀♦ Literals
∀♦ Comments
∀♦ Expressions and comparisons
∀♦ Data types and Declarations
(i) Arithmetic operators and symbols:
Operator Examples Description
Operator Operation
♦ %ROWTYPE
%TYPE
In general, the variables that deal with table columns
should have the same data type and length as the column
itself. %type attribute is used when declaring variables
that refer to the database columns. When using the
%type keyword, all you need to know is the name of the
column and the table to which the variable will correspond.
DECLARE
eno emp.empno%type;
salary emp.sal%type;
BEGIN
………..
END;
The advantages of using %TYPE are as follows:
♦ We need not know the exact data type of the
column empno and sal.
♦ If the database definition of empno and sal is
changed, then, the data type of eno and salary changes
accordingly at run time.
DECLARE
my_empid employee.empid%TYPE;
my_lastname employee.lastname%TYPE;
my_firstname employee.firstname%TYPE;
my_salary employee.salary%TYPE;
BEGIN
……..
END;
%ROWTYPE
%rowtype attribute provides a record type that
represents a row in a table. For example, if the
EMPLOYEE table contains four columns—EMPID,
LASTNAME, FIRSTNAME, and SALARY—and you want to
manipulate the values in each column of a row using only
one referenced variable, the variable can be declared with
the %rowtype keyword. Compare the use of %rowtype to
manual record declaration:
DECLARE
my_employee employee%ROWTYPE;
BEGIN
…..
END;
BY USING % ROWTYPE
DECLARE
REC EMP1%ROWTYPE;
T NUMBER(4);
BEGIN
SELECT * INTO REC FROM EMP WHERE
EMPNO=100;
T:=REC.BP+REC.HRA+REC.DA;
UPDATE EMP1 SET TOTOAL=T WHERE
EMPNO=100;
END;
Displaying user Messages on the Screen