PL/SQL
CONTENTS:
PL/SQL BASICS
A N O N Y M O U S B LO C K
S CONTROL STRUC
TURES
Dr. Yogita Pagar (Bhise)
DEPARTMENT OF COMPUTER ENGINEERING
K.K.W.I.E.E.R., Nashik
8/20/2022
Session Outcomes
At the end of this session, you will be able to:
● Understand the PL/SQL Block terminology
and features of PL/SQL
● Use various PL/SQL types, variables and
subtypes
● Use SQL statements inside PL/SQL Block
● Use various PL/SQL control structures
● Write and execute anonymous Blocks
8/20/2022 2
SQL
● The purpose of SQL is to provide an interface to a
relational database such as Oracle Database, and
all SQL statements are instructions to the
database.
● The strengths of SQL provide benefits for all types
of users, including application programmers,
database administrators, managers, and end
users.
8/20/2022 3
Why PLSQL
If SQL is so
powerful,
why we
require to
use
PL/SQL?
Let us think
about few
scenarios…
8/20/2022 4
Why PLSQL
How can we have a chain of SQL statements
which produce result according to the output of
previous queries?
How can we take any smart decisions based on users
input or based on output on previous queries..?
How can we automate any task using SQL?
8/20/2022 5
Introduction
• The PL/SQL programming language was developed
by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the
Oracle relational database.
• PL/SQL, supplement SQL with standard
programming- language features like:
• Block (modular) structure
• Flow-control statements and loops
• Variables, constants, and types
• Structured data
• Customized error handling
8/20/2022 6
Why
PL/SQL?
• The purpose of PL/SQL is to combine database
language and procedural programming language.
• By extending SQL, PL/SQL offers a unique
combination of power and ease of use.
• PL/SQL fully supports all SQL data
manipulation statements.
• We can write procedures and functions which can
be invoked from different applications.
8/20/2022 7
Why
PL/SQL?
● provides facilities like condition checking,
branching, looping.
● Entire block of SQL statements to engine reduces
network traffic
● Permits dealing with errors
● Use of variables
8/20/2022 8
Why
PL/SQL? Tight
Integration
with SQL
Access to
Better
Pre-defined
Performance
Packages
Supports
Tight Security both
static and
Advantages dynamic SQL.
Of
PL/SQL
Support for
Object- Higher
Oriented Productivity
Programming
Support for
Developing
Full Portability
Web
Applications
8/20/2022
PL/SQL
Block
• PL/SQL is a block-structured language.
• Each program written in PL/SQL is written as a
block.
• Blocks can also be nested.
• Each block is meant for a particular task.
PL/SQL
Block types
Anonymous Named
Block Block
8/20/2022
PL/SQL
Block
Anonymous Block Named Block
□Anonymous block doesn’t □Named block do have
have name. names.
□After compilation, it is not □Once compiled, Named
getting stored on oracle blocks are getting stored
server. on oracle server.
□We can not call □We can call them in ay
anonymous block in any other pl/sql block
other pl/sql block
8/20/2022
PL/SQL Anonymous
Block
8/20/2022
Comments in
PL/SQL
• To comment line or lines in a PL/SQL program
use,
• -- for single line comment
• /* */ for multi-line comment
8/20/2022
First PL/SQL
Block
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Hello! Welcome to PL/SQL’);
END;
• DBMS_OUTPUT is a package which is used for
debugging, or for displaying messages SQL*Plus
• PUT_LINE is a procedure within that package; it accepts
a line of information as input
8/20/2022
PL/SQL Control
Structures
● PL/SQL, like other 3GL has a variety of control
structures which include
• Conditional controls
o IF
• Iterative Controls (Loops)
o Simple loop
o While loop
o For loop
• Sequential control
o GOTO
8/20/2022
Conditional controls IF
● Syntax
IF condition 1 THEN
-- Executes when the condition 1 is true
Statements;
ELSIF condition 2 THEN
-- Executes when the condition 2 is true
Statements;
ELSIF condition3 THEN
-- Executes when the condition 3 is true
Statements;
ELSE
-- executes when the none of the above
condition is true
Statements;
END IF;
8/20/2022
Example: IF .. ELSE ..
END IF
Declare
mroll
number(10); matt
number(10);
Begin
mroll:= &mroll;
select at into matt from stud11 where roll =
mroll; if matt<75 then
dbms_output.put_line(mroll||'is detained');
update stud11 set status='D' where roll=mroll;
else
dbms_output.put_line(mroll||'is Not detained');
update stud11 set status='ND'where roll=mroll;
end if;
End; 8/20/2022
Thank You
8/20/2022