Introduction To PL/SQL: Stone Apple Solutions Pte LTD
Introduction To PL/SQL: Stone Apple Solutions Pte LTD
2/28/2018 1
Agenda
Day 3
• PL/SQL Introduction
• PL/SQL Advantages
• PL/SQL Block Structure
• PL/SQL Data Type
• PL/SQL Variable & Scope
• PL/SQL DML
Day 4
• PL/SQL Conditional Control
• PL/SQL Loop Statement
• PL/SQL Case Statement
• PL/SQL Cursor
• PL/SQL Exception
Introduction PL/SQL
PL/SQL Advantages
PL/SQL is a Procedural Language extension of Structured
Query Language (SQL).
PL/SQL is specially designed for Database oriented activities.
Oracle PL/SQL allows you to perform data manipulation
operation those are safe and flexible.
PL/SQL is a very secure functionality tool for manipulating,
controlling, validating, and restricting unauthorized access
data from the SQL database.
Using PL/SQL we can improve application performance. It
also allows to deal with errors so we can provide user friendly
error messages.
PL/SQL have a great functionality to display multiple records
from the multiple tables at the same time.
PL/SQL Advantages
PL/SQL is capable to send entire block of statements and execute it in
the Oracle engine at once.
Advantages PL/SQL
• Procedural language support : PL/SQL is a development tools not
only for data manipulation futures but also provide the
conditional checking, looping or branching operations same as like
other programming language.
• Reduces network traffic : This one is great advantages of PL/SQL.
Because PL/SQL nature is entire block of SQL statements execute
into oracle engine all at once so it's main benefit is reducing the
network traffic.
• Error handling : PL/SQL is dealing with error handling, It's permits
the smart way handling the errors and giving user friendly error
messages, when the errors are encountered.
PL/SQL Advantages
• Declare variable : PL/SQL gives you control to declare
variables and access them within the block. The
declared variables can be used at the time of query
processing.
• Intermediate Calculation : Calculations in PL/SQL done
quickly and efficiently without using Oracle engines.
This improves the transaction performance.
• Portable application : Applications are written in
PL/SQL are portable in any Operating system. PL/SQL
applications are independence program to run any
computer.
PL/SQL Block Structure
PL/SQL Block Structure
PL/SQL Block Structure
PL/SQL is block structured language divided into
three logical blocks.
BEGIN block and END; keyword are compulsory,
and other two block DECLARE and EXCEPTION
are optional block. END; is not a block only
keyword to end of PL/SQL program.
PL/SQL block structure follows divide-and-
conquer approach to solve the problem
stepwise.
PL/SQL Block Structure
DECLARE
Variables and constants are declared, initialized within this section.
Variables and Constants : In this block, declare and initialize variables (and
constants). You must have to declare variables and constants in declarative
block before referencing them in procedural statement.
PL/SQL Block Structure
Declare Variables and Assigning values : You can
define variable name, data type of a variable
and its size. Date type can be: CHAR, VARCHAR2,
DATE, NUMBER, INT or any other.
PL/SQL Block Structure
Declare Constants and Assigning values :
Constants are declared same as variable but you
have to add the CONSTANT keyword before
defining data type. Once you define a constant
value you can't change the value.
PL/SQL Block Structure
BEGIN/END
BEGIN block is procedural statement block
which will implement the actual programming
logic. This section contains conditional
statements (if...else), looping statements (for,
while) and Branching Statements (goto) etc.
PL/SQL Block Structure
EXCEPTION
PL/SQL easily detects user defined or predefined
error condition. PL/SQL is famous for handling
errors in smart way by giving suitable user
friendly messages. Errors can be rise due to
wrong syntax, bad logical or not passing a
validation rules.
PL/SQL Block Structure
You can also define exception in your declarative block and later you can
execute it by RAISE statement.
Note :
BEGIN block and END; keyword are compulsory of any PL/SQL program.
Where as DECLARE and EXCEPTION block are optional.
PL/SQL Block Structure
PL/SQL Block Structure
• Procedure and Function Can be call through
Anonymous Block
Ini contoh yang prosedure
Begin
my_procedure(<argument>);
End;
Variables Scope
PL/SQL variable scope is identified the region range
which you can reference the variable. PL/SQL have
two type scopes local scope and global scope,
PL/SQL Variable & Scope
• Transaction Control
– COMMIT
– ROLLBACK
Exercise
• IF THEN Statement
• IF THEN ELSE Statement
• IF THEN ELSIF Statement
• Nested IF THEN ELSE Statement
PL/SQL Conditional Control
IF THEN Statement
IF THEN Statement write in following syntax format:
IF ( condition ) THEN
Statement
END IF;
Example
We declare one number with initialize 14 value is equal of condition value,
Comparing 2 values by using IF THEN statement,
PL/SQL Conditional Control
Example
Same as above example if condition not true then else part will execute.
PL/SQL Conditional Control
Example
Here one student result example for archiving grade.
PL/SQL Conditional Control
Example
Here check condition students gender male, if not male then finding the
result using nested IF THEN ELSE statement.
PL/SQL Conditional Control
Exercise:
Create PL/SQL program to Check student passing grade. If value between 9
and 10 then ‘A grade’, if value between 7 and 8 then ‘B grade’, if value is 6
then ‘C grade’, and others is ‘D grade’. Let assume the value is ‘7’.
Put the passing grade value in declare variable
Syntax
CASE selector
WHEN value-1
THEN statement-1;
WHEN value-2
THEN statement-2;
ELSE
statement-3;
END CASE
PL/SQL Case Statement
Syntax
CASE WHEN condition-1 THEN
statement-1;
WHEN condition-2 THEN
statement-2;
ELSE
statement-3;
END CASE;
PL/SQL Case Statement
Result :
value of Amount : 1000 discount 5%
PL/SQL Cursor
PL/SQL Cursor
Cursor is the work area which Oracle reserves for
internal processing of SQL statements. This work
area is private for oracles reserved are called cursor.
Classification of CURSORS
Cursors can be classified as:
• Implicit Cursor or Internal Cursor : Manage for
Oracle itself or internal process itself.
• Explicit Cursor or User-defined Cursor : Manage
for user/programmer or external processing.
PL/SQL Implicit Cursor
Syntax
cursor_attribute ::=
{
cursor_name |
cursor_variable_name |
:host_cursor_variable_name
} % {FOUND | NOTFOUND | ROWCOUNT}
PL/SQL Implicit Cursor
Explanation :
• cursor_name : cursor_name identifies the
current scope which are previously declared.
• cursor_variable_name : cursor variable or
parameter identifies the current scope which are
previously declared.
• host_cursor_variable_name :
host_cursor_variable_name must be prefixed
with a colon. Host cursor variable datatype must
be compatible with the PL/SQL cursor variable.
PL/SQL Implicit Cursor
Example:
PL/SQL Implicit Cursor
Exercise:
Create PL/SQL Implicit Cursor to test whether item
name ‘Playstation’ exists on items table.
The output should be like below:
PL/SQL Explicit Cursor
Example:
PL/SQL Explicit Cursor
Exercise:
Create PL/SQL explicit cursor to update existing amount by
adding ten percent on orders table.
The output should be like below:
PL/SQL Cursor For Loop
Syntax
DECLARE
declaration statement(s);
BEGIN
statement(s);
EXCEPTION
WHEN built-in_exception_name_1 THEN
User defined statement (action) will be taken;
WHEN built-in_exception_name_2 THEN
User defined statement (action) will be taken;
END;
PL/SQL Exception
Example:
PL/SQL Exception
• To get detail error
from SQLERRM variable
Begin
---
Exception
when others then
dbms_output.put_line(SQLERRM);
End;
Exercise
Exercise:
Create PL/SQL Exception with ‘Too_many_rows’.
The output should be like below:
Exercise
Exercise:
Create PL/SQL Exception with ‘OTHERS’.
Display the error using SQLERRM