SlideShare a Scribd company logo
RDBMS - Unit V
Chapter 15
PL/SQL
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
Alagappa Government Arts College, Karaikudi.
(Affiliated by Alagappa University)
Mailid: muruganjit@gmail.com
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO
PL/SQL
➢ PL/SQL is an extension of the SQL language.
➢ It is a procedural language.
➢ PL/SQL combines the SQL language’s ease of data
manipulation and the procedural language’s ease of
programming.
➢ With the PL/SQL language, we can create schema
objects, including Stored procedures and functions,
Packages, Triggers, Cursors.
PL/SQL
➢ Stored procedures and functions. A stored procedure
is a PL/SQL program that can be enabled by an
application, a trigger, or an Oracle tool.
➢ The basic difference between a procedure and a
function is that a procedure executes its commands
and a function executes commands and returns a
result.
➢ Packages. A package is a file that contains a group of
functions, cursors, stored procedures, and variables in
one place.
PL/SQL
➢ Triggers. A trigger is a PL/SQL program that is stored
in the database and executed immediately before or
after the INSERT, UPDATE, and DELETE
commands.
➢ Cursors. Oracle uses workspaces to execute the SQL
commands. Through PL/SQL cursors, it is possible to
name the workspace and access its information.
PL/SQL BLOCK
➢ The basic structure of PL/SQL block is given below:
PL/SQL BLOCK
There are three parts to the PL/SQL block:
➢ Declaration section/Declare (optional), in which all
the objects are declared.
➢ Execution section/Begin, in which the PL/SQL
commands are placed.
➢ Exception section/Exception (optional), in which the
errors are handled.
PL/SQL BLOCK - Declaration Section
In the declaration section, the developer can perform the
following:
➢ Declare the name of an identifier - PI.
➢ Declare the type of identifier (constant or variable).
➢ Declare the data type of the identifier. - REAL
➢ Assign (initialize) contents to the identifier. – 3.14
Ex: PI CONSTANT REAL := 3.14
PL/SQL BLOCK - Variables
➢ The variables can contain any data type that is valid
for SQL and Oracle (such as char, number, long,
varchar2, and date) in addition to these types:
➢ Boolean Can be assigned the values True, False, or
NULL.
➢ Binary_integer Accepts numbers between
-2,147,483,647 and 2,147,483,647.
➢ Positive Accepts numbers from 1 to 2,147,483,647.
➢ Natural Accepts numbers from 0 to 2,147,483,647.
PL/SQL BLOCK - Variables
➢ %type Assigns to the variable that is being created the
same data types used by the column that is being used.
To access the field of deptno from dept. table and
assigned to deptp.
deptp := dept.deptno%type;
➢ %rowtype Declares a composed variable that is
equivalent to the row of the table.
➢ After the variable is created, the fields of the table can
be accessed, using the name of this variable followed
by a period and the name of the field:
Empline := emp%rowtype
PL/SQL BLOCK - Variables
➢ After the variable is created, you can use the following
expression to assign a column to another variable:
Newvar : = empline.ename;
PL/SQL BLOCK - Variables
➢ There are two ways to assign values to a variable. The
first is to use the assignment operator “:=”:
tot := price * margin;
increase:= sal * 1.10;
➢ The second way to assign values to variables is to use
the SELECT command to assign the contents of the
fields of a table to a variable:
SELECT sal * 0.10 INTO increased FROM
emp WHERE empno = emp_id;
PL/SQL BLOCK - Constant
➢ The declaration of a constant is similar to the
declaration of a variable, except to add the keyword
Constant after the name of the constant:
PI CONSTANT REAL := 3.14;
➢ Each variable or constant must be specified with its
name, type, and, optionally, initial value. All the rows
must end with a semicolon:
DECLARE
Cust_name varchar2 (20);
Credit number (5,2) : = 100;
PL/SQL BLOCK - Execution Section
➢ This section begins with the Begin declaration.
➢ This section can contain SQL commands, logical
control commands, and assignment commands, as well
as other commands.
➢ All commands are always end with a semicolon.
PL/SQL BLOCK - Exception
➢ In this section, the developer can use commands to
handle an error that occurs during the execution of a
PL/SQL program.
How PL/SQL Works
➢ PL/SQL is an engine that makes up part of the Oracle
server.
➢ It executes the procedural commands and passes the
SQL commands for the Oracle server to process.
➢ PL/SQL blocks can be created with any of the
processing editors. (Ex: Windows Notepad)
➢ To execute a PL/SQL program or script, you can use
SQL*Plus, which allows creating, storing, and
executing PL/SQL blocks.
How PL/SQL Works
Example Program
➢ Step 1: Type the program in editor and execute using
@ symbol.
➢ SQL> set serveroutput on;
Example Program for variable and constant
Control Structures
➢ PL/SQL has some commands to control the execution
flow of the program. They are responsible for decision
making inside the application.
➢ control structures in PL/SQL, which can be divided
into condition control structures (selection), sequence
structures, and repetition or iteration structures.
➢ Different types of structure explained in the figure
15.3.
Control Structures
The IF..THEN Command
➢ The IF..THEN command evaluates a condition and
executes one or more rows of commands only if the
analyzed condition is true.
➢ It has two variations as follows:
The IF..THEN Command
➢ In syntax1, the commands that appear between the
THEN and END IF clauses will be executed only if
the condition is true.
Example:
declare
sal number(7,2);
begin
select salary into sal from emp2 where eno=101;
IF SAL>= 5000 THEN
UPDATE EMP2 SET salary = salary*1.2;
END IF;
dbms_output.put_line(sal);
end;
/
The IF..THEN Command
In syntax2, more than one condition can be analyzed
and, therefore, several actions can be executed:
1. create table emp2(eno number(3), salary number(7,2))
2. insert into emp2 values (102, 1500)
3. Type the following program and execute:
declare
sal number(7,2);
begin
select salary into sal from emp2 where eno=101;
IF sal<2000 THEN
UPDATE EMP2 SET salary = salary * 1.2;
ELSIF sal <3000 THEN
UPDATE EMP2 SET salary = salary * 1.3;
ELSE
UPDATE EMP2 SET salary = salary * 1.4;
END IF;
dbms_output.put_line(sal);
end;
The IF..THEN Command
➢ If the main condition is true, the following commands
will be executed until another ELSIF or ELSE clause
is found.
➢ If the first ELSIF condition is false, the program tests
the second condition, and so on.
➢ When the first true condition is found, its commands
are executed, and the program jumps to the row after
the END IF command.
The LOOP Command
➢ The LOOP command initializes a group of commands
indefinitely, or until a condition forces a “break” in the
loop and the execution of the program to another
place.
➢ The EXIT command is responsible for interrupting the
loop execution:
The LOOP Command - Example
LOOP with IF Loop with WHEN
declare
I number(3);
begin
I := 1;
Loop
I := I+1;
DBMS_OUTPUT.PUT_LINE(I);
IF I>= 30 THEN
EXIT;
END IF;
END LOOP;
END;
/
declare
I number(3);
begin
I := 1;
Loop
I := I+1;
DBMS_OUTPUT.PUT_LINE(I);
EXIT WHEN I>= 30;
END LOOP;
END;
/
➢ The following loop repeated until the counter variable
I become 30.
The FOR..LOOP Command
➢ The FOR..LOOP command is a variation of the LOOP
command.
➢ Here, the commands are automatically executed until
an evaluated condition returns false.
Syntax:
FOR <counter> IN [REVERSE] <initial_value> . . <final_value>
LOOP
<commands>
END LOOP;
The FOR LOOP Command - Example
Example:
declare
j number(3);
begin
FOR j IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(j);
END LOOP;
END;
/
➢ The FOR command initializes a control variable called J, which
has the initial value of 1.
➢ The commands will be executed until END LOOP is found.
➢ Then, the control returns to the FOR command, which
increments the variable and analyzes the master condition, i.e.,
whether the value of J is less than the final value.
➢ When this happens the cycle is reinitialized.
The FOR LOOP Command - Example
➢ The REVERSE clause makes the counter start with the
highest value and decrease until it reaches the lowest
value.
declare
j number(3);
begin
FOR j IN reverse 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(j);
END LOOP;
END;
/
The WHILE Command
➢ The WHILE command is another control structure.
This structure only executes the commands if the
analyzed condition is true.
declare
x number(3);
begin
x:=1;
while x<=20 loop
DBMS_OUTPUT.PUT_LINE(x);
x:=x+1;
END LOOP;
END;
/
Integrating SQL in a PL/SQL Program
➢ SQL commands can be inserted in the execution
section of a PL/SQL block.
➢ The developer can use (reference) a variable or
constant declared in the declaration section.
➢ Inside a PL/SQL program the SELECT command uses
a new clause called INTO that allows transferring the
contents of the fields in a row to the variables in
memory.
➢ Command: SET SERVEROUTPUT ON, GET
(Retreive the file), RUN (@ Execute the program)
Integrating SQL in a PL/SQL Program
Declare
Var_name number (7,2) ;
Begin
SELECT salary INTO Var_name FROM emp2 where eno
= 101;
dbms_output.put_line('salary=' || Var_name);
End;
/
➢ Note that to attribute contents to a field in a variable,
the SELECT command must return only one row;
➢ otherwise an error will occur, since several values are
being assigned to only one variable.

More Related Content

Similar to Lecture Notes Unit5 chapter 15 PL/SQL Programming

PLSQL
PLSQLPLSQL
Plsql
PlsqlPlsql
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
faizan992426
 
Module04
Module04Module04
Module04
Sridhar P
 
Plsql
PlsqlPlsql
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
mahalakshmimalini
 
Assignment#08
Assignment#08Assignment#08
Assignment#08
Sunita Milind Dol
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
priyaprakash11
 
Stored procedure
Stored procedureStored procedure
Stored procedure
Stored procedureStored procedure
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
Dhilip Prakash
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
Dhilip Prakash
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna19
 
rdbms.pdf plsql database system notes for students to study
rdbms.pdf plsql database system notes for students to studyrdbms.pdf plsql database system notes for students to study
rdbms.pdf plsql database system notes for students to study
rarelyused
 
Oracle pl sql
Oracle pl sqlOracle pl sql
Oracle pl sql
Durga Rao
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
Reka
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
ASHABOOPATHY
 

Similar to Lecture Notes Unit5 chapter 15 PL/SQL Programming (20)

PLSQL
PLSQLPLSQL
PLSQL
 
Plsql
PlsqlPlsql
Plsql
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
Module04
Module04Module04
Module04
 
Plsql
PlsqlPlsql
Plsql
 
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
 
Assignment#08
Assignment#08Assignment#08
Assignment#08
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
rdbms.pdf plsql database system notes for students to study
rdbms.pdf plsql database system notes for students to studyrdbms.pdf plsql database system notes for students to study
rdbms.pdf plsql database system notes for students to study
 
Oracle pl sql
Oracle pl sqlOracle pl sql
Oracle pl sql
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
 

Recently uploaded

Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
Celine George
 
Understanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooksUnderstanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooks
TechSoup
 
Module 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.pptModule 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.ppt
KIPAIZAGABAWA1
 
Introduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptxIntroduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptx
jessintv
 
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdfFINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
Janna Marie Ballo
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
Celine George
 
How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17
Celine George
 
Brigada Eskwela editable Certificate.pptx
Brigada Eskwela editable Certificate.pptxBrigada Eskwela editable Certificate.pptx
Brigada Eskwela editable Certificate.pptx
aiofits06
 
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry  by V.Jesinthal MaryPlato and Aristotle's Views on Poetry  by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
jessintv
 
BANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
BANG E BHARAT QSN SET by Amra Quiz Pagoler DolBANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
BANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
Amra Quiz Pagoler Dol (AQPD)
 
Types of Diode and its working principle.pptx
Types of Diode and its working principle.pptxTypes of Diode and its working principle.pptx
Types of Diode and its working principle.pptx
nitugatkal
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
Celine George
 
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptxTale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
OH TEIK BIN
 
How to Use Quality Module in Odoo 17 - Odoo 17 Slides
How to Use Quality Module in Odoo 17 - Odoo 17 SlidesHow to Use Quality Module in Odoo 17 - Odoo 17 Slides
How to Use Quality Module in Odoo 17 - Odoo 17 Slides
Celine George
 
SD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptxSD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptx
MarkKennethBellen1
 
Java Developer Roadmap PDF By ScholarHat
Java Developer Roadmap PDF By ScholarHatJava Developer Roadmap PDF By ScholarHat
Java Developer Roadmap PDF By ScholarHat
Scholarhat
 
Celebrating 25th Year SATURDAY, 27th JULY, 2024
Celebrating 25th Year SATURDAY, 27th JULY, 2024Celebrating 25th Year SATURDAY, 27th JULY, 2024
Celebrating 25th Year SATURDAY, 27th JULY, 2024
APEC Melmaruvathur
 
How to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POSHow to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POS
Celine George
 
How to Configure Extra Steps During Checkout in Odoo 17 Website App
How to Configure Extra Steps During Checkout in Odoo 17 Website AppHow to Configure Extra Steps During Checkout in Odoo 17 Website App
How to Configure Extra Steps During Checkout in Odoo 17 Website App
Celine George
 
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
OliverVillanueva13
 

Recently uploaded (20)

Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
 
Understanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooksUnderstanding Clergy Payroll : QuickBooks
Understanding Clergy Payroll : QuickBooks
 
Module 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.pptModule 5 Bone, Joints & Muscle Injuries.ppt
Module 5 Bone, Joints & Muscle Injuries.ppt
 
Introduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptxIntroduction to Literary Criticism 10 (1).pptx
Introduction to Literary Criticism 10 (1).pptx
 
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdfFINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
 
How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17
 
Brigada Eskwela editable Certificate.pptx
Brigada Eskwela editable Certificate.pptxBrigada Eskwela editable Certificate.pptx
Brigada Eskwela editable Certificate.pptx
 
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry  by V.Jesinthal MaryPlato and Aristotle's Views on Poetry  by V.Jesinthal Mary
Plato and Aristotle's Views on Poetry by V.Jesinthal Mary
 
BANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
BANG E BHARAT QSN SET by Amra Quiz Pagoler DolBANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
BANG E BHARAT QSN SET by Amra Quiz Pagoler Dol
 
Types of Diode and its working principle.pptx
Types of Diode and its working principle.pptxTypes of Diode and its working principle.pptx
Types of Diode and its working principle.pptx
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
 
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptxTale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
Tale of a Scholar and a Boatman ~ A Story with Life Lessons (Eng. & Chi.).pptx
 
How to Use Quality Module in Odoo 17 - Odoo 17 Slides
How to Use Quality Module in Odoo 17 - Odoo 17 SlidesHow to Use Quality Module in Odoo 17 - Odoo 17 Slides
How to Use Quality Module in Odoo 17 - Odoo 17 Slides
 
SD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptxSD_Instructional-Design-Frameworkzz.pptx
SD_Instructional-Design-Frameworkzz.pptx
 
Java Developer Roadmap PDF By ScholarHat
Java Developer Roadmap PDF By ScholarHatJava Developer Roadmap PDF By ScholarHat
Java Developer Roadmap PDF By ScholarHat
 
Celebrating 25th Year SATURDAY, 27th JULY, 2024
Celebrating 25th Year SATURDAY, 27th JULY, 2024Celebrating 25th Year SATURDAY, 27th JULY, 2024
Celebrating 25th Year SATURDAY, 27th JULY, 2024
 
How to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POSHow to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POS
 
How to Configure Extra Steps During Checkout in Odoo 17 Website App
How to Configure Extra Steps During Checkout in Odoo 17 Website AppHow to Configure Extra Steps During Checkout in Odoo 17 Website App
How to Configure Extra Steps During Checkout in Odoo 17 Website App
 
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
21stcenturyskillsframeworkfinalpresentation2-240509214747-71edb7ee.pptx
 

Lecture Notes Unit5 chapter 15 PL/SQL Programming

  • 1. RDBMS - Unit V Chapter 15 PL/SQL Prepared By Dr. S.Murugan, Associate Professor Department of Computer Science, Alagappa Government Arts College, Karaikudi. (Affiliated by Alagappa University) Mailid: muruganjit@gmail.com Reference Book: LEARN ORACLE 8i, JOSE A RAMALHO
  • 2. PL/SQL ➢ PL/SQL is an extension of the SQL language. ➢ It is a procedural language. ➢ PL/SQL combines the SQL language’s ease of data manipulation and the procedural language’s ease of programming. ➢ With the PL/SQL language, we can create schema objects, including Stored procedures and functions, Packages, Triggers, Cursors.
  • 3. PL/SQL ➢ Stored procedures and functions. A stored procedure is a PL/SQL program that can be enabled by an application, a trigger, or an Oracle tool. ➢ The basic difference between a procedure and a function is that a procedure executes its commands and a function executes commands and returns a result. ➢ Packages. A package is a file that contains a group of functions, cursors, stored procedures, and variables in one place.
  • 4. PL/SQL ➢ Triggers. A trigger is a PL/SQL program that is stored in the database and executed immediately before or after the INSERT, UPDATE, and DELETE commands. ➢ Cursors. Oracle uses workspaces to execute the SQL commands. Through PL/SQL cursors, it is possible to name the workspace and access its information.
  • 5. PL/SQL BLOCK ➢ The basic structure of PL/SQL block is given below:
  • 6. PL/SQL BLOCK There are three parts to the PL/SQL block: ➢ Declaration section/Declare (optional), in which all the objects are declared. ➢ Execution section/Begin, in which the PL/SQL commands are placed. ➢ Exception section/Exception (optional), in which the errors are handled.
  • 7. PL/SQL BLOCK - Declaration Section In the declaration section, the developer can perform the following: ➢ Declare the name of an identifier - PI. ➢ Declare the type of identifier (constant or variable). ➢ Declare the data type of the identifier. - REAL ➢ Assign (initialize) contents to the identifier. – 3.14 Ex: PI CONSTANT REAL := 3.14
  • 8. PL/SQL BLOCK - Variables ➢ The variables can contain any data type that is valid for SQL and Oracle (such as char, number, long, varchar2, and date) in addition to these types: ➢ Boolean Can be assigned the values True, False, or NULL. ➢ Binary_integer Accepts numbers between -2,147,483,647 and 2,147,483,647. ➢ Positive Accepts numbers from 1 to 2,147,483,647. ➢ Natural Accepts numbers from 0 to 2,147,483,647.
  • 9. PL/SQL BLOCK - Variables ➢ %type Assigns to the variable that is being created the same data types used by the column that is being used. To access the field of deptno from dept. table and assigned to deptp. deptp := dept.deptno%type; ➢ %rowtype Declares a composed variable that is equivalent to the row of the table. ➢ After the variable is created, the fields of the table can be accessed, using the name of this variable followed by a period and the name of the field: Empline := emp%rowtype
  • 10. PL/SQL BLOCK - Variables ➢ After the variable is created, you can use the following expression to assign a column to another variable: Newvar : = empline.ename;
  • 11. PL/SQL BLOCK - Variables ➢ There are two ways to assign values to a variable. The first is to use the assignment operator “:=”: tot := price * margin; increase:= sal * 1.10; ➢ The second way to assign values to variables is to use the SELECT command to assign the contents of the fields of a table to a variable: SELECT sal * 0.10 INTO increased FROM emp WHERE empno = emp_id;
  • 12. PL/SQL BLOCK - Constant ➢ The declaration of a constant is similar to the declaration of a variable, except to add the keyword Constant after the name of the constant: PI CONSTANT REAL := 3.14; ➢ Each variable or constant must be specified with its name, type, and, optionally, initial value. All the rows must end with a semicolon: DECLARE Cust_name varchar2 (20); Credit number (5,2) : = 100;
  • 13. PL/SQL BLOCK - Execution Section ➢ This section begins with the Begin declaration. ➢ This section can contain SQL commands, logical control commands, and assignment commands, as well as other commands. ➢ All commands are always end with a semicolon.
  • 14. PL/SQL BLOCK - Exception ➢ In this section, the developer can use commands to handle an error that occurs during the execution of a PL/SQL program.
  • 15. How PL/SQL Works ➢ PL/SQL is an engine that makes up part of the Oracle server. ➢ It executes the procedural commands and passes the SQL commands for the Oracle server to process. ➢ PL/SQL blocks can be created with any of the processing editors. (Ex: Windows Notepad) ➢ To execute a PL/SQL program or script, you can use SQL*Plus, which allows creating, storing, and executing PL/SQL blocks.
  • 17. Example Program ➢ Step 1: Type the program in editor and execute using @ symbol. ➢ SQL> set serveroutput on;
  • 18. Example Program for variable and constant
  • 19. Control Structures ➢ PL/SQL has some commands to control the execution flow of the program. They are responsible for decision making inside the application. ➢ control structures in PL/SQL, which can be divided into condition control structures (selection), sequence structures, and repetition or iteration structures. ➢ Different types of structure explained in the figure 15.3.
  • 21. The IF..THEN Command ➢ The IF..THEN command evaluates a condition and executes one or more rows of commands only if the analyzed condition is true. ➢ It has two variations as follows:
  • 22. The IF..THEN Command ➢ In syntax1, the commands that appear between the THEN and END IF clauses will be executed only if the condition is true. Example: declare sal number(7,2); begin select salary into sal from emp2 where eno=101; IF SAL>= 5000 THEN UPDATE EMP2 SET salary = salary*1.2; END IF; dbms_output.put_line(sal); end; /
  • 23. The IF..THEN Command In syntax2, more than one condition can be analyzed and, therefore, several actions can be executed: 1. create table emp2(eno number(3), salary number(7,2)) 2. insert into emp2 values (102, 1500) 3. Type the following program and execute: declare sal number(7,2); begin select salary into sal from emp2 where eno=101; IF sal<2000 THEN UPDATE EMP2 SET salary = salary * 1.2; ELSIF sal <3000 THEN UPDATE EMP2 SET salary = salary * 1.3; ELSE UPDATE EMP2 SET salary = salary * 1.4; END IF; dbms_output.put_line(sal); end;
  • 24. The IF..THEN Command ➢ If the main condition is true, the following commands will be executed until another ELSIF or ELSE clause is found. ➢ If the first ELSIF condition is false, the program tests the second condition, and so on. ➢ When the first true condition is found, its commands are executed, and the program jumps to the row after the END IF command.
  • 25. The LOOP Command ➢ The LOOP command initializes a group of commands indefinitely, or until a condition forces a “break” in the loop and the execution of the program to another place. ➢ The EXIT command is responsible for interrupting the loop execution:
  • 26. The LOOP Command - Example LOOP with IF Loop with WHEN declare I number(3); begin I := 1; Loop I := I+1; DBMS_OUTPUT.PUT_LINE(I); IF I>= 30 THEN EXIT; END IF; END LOOP; END; / declare I number(3); begin I := 1; Loop I := I+1; DBMS_OUTPUT.PUT_LINE(I); EXIT WHEN I>= 30; END LOOP; END; / ➢ The following loop repeated until the counter variable I become 30.
  • 27. The FOR..LOOP Command ➢ The FOR..LOOP command is a variation of the LOOP command. ➢ Here, the commands are automatically executed until an evaluated condition returns false. Syntax: FOR <counter> IN [REVERSE] <initial_value> . . <final_value> LOOP <commands> END LOOP;
  • 28. The FOR LOOP Command - Example Example: declare j number(3); begin FOR j IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(j); END LOOP; END; / ➢ The FOR command initializes a control variable called J, which has the initial value of 1. ➢ The commands will be executed until END LOOP is found. ➢ Then, the control returns to the FOR command, which increments the variable and analyzes the master condition, i.e., whether the value of J is less than the final value. ➢ When this happens the cycle is reinitialized.
  • 29. The FOR LOOP Command - Example ➢ The REVERSE clause makes the counter start with the highest value and decrease until it reaches the lowest value. declare j number(3); begin FOR j IN reverse 1..10 LOOP DBMS_OUTPUT.PUT_LINE(j); END LOOP; END; /
  • 30. The WHILE Command ➢ The WHILE command is another control structure. This structure only executes the commands if the analyzed condition is true. declare x number(3); begin x:=1; while x<=20 loop DBMS_OUTPUT.PUT_LINE(x); x:=x+1; END LOOP; END; /
  • 31. Integrating SQL in a PL/SQL Program ➢ SQL commands can be inserted in the execution section of a PL/SQL block. ➢ The developer can use (reference) a variable or constant declared in the declaration section. ➢ Inside a PL/SQL program the SELECT command uses a new clause called INTO that allows transferring the contents of the fields in a row to the variables in memory. ➢ Command: SET SERVEROUTPUT ON, GET (Retreive the file), RUN (@ Execute the program)
  • 32. Integrating SQL in a PL/SQL Program Declare Var_name number (7,2) ; Begin SELECT salary INTO Var_name FROM emp2 where eno = 101; dbms_output.put_line('salary=' || Var_name); End; / ➢ Note that to attribute contents to a field in a variable, the SELECT command must return only one row; ➢ otherwise an error will occur, since several values are being assigned to only one variable.