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.
Ad

Recommended

Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Murugan Solaiyappan
 
Lecture Notes Unit1 chapter1 Introduction
Lecture Notes Unit1 chapter1 Introduction
Murugan146644
 
Object Based Databases
Object Based Databases
Farzad Nozarian
 
Database Chapter 3
Database Chapter 3
shahadat hossain
 
Sql.pptx
Sql.pptx
TanishaKochak
 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
shalinigambhir3
 
sql function(ppt)
sql function(ppt)
Ankit Dubey
 
Package Diagram
Package Diagram
WASI ALI
 
Ppt of dbms e r features
Ppt of dbms e r features
Nirali Akabari
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints
Kumar
 
Entity Relationship Model
Entity Relationship Model
Slideshare
 
SQL Constraints
SQL Constraints
Randy Riness @ South Puget Sound Community College
 
DBMS unit-3.pdf
DBMS unit-3.pdf
Prof. Dr. K. Adisesha
 
Data Models.ppt
Data Models.ppt
AnshikaGoel42
 
Rehashing
Rehashing
rajshreemuthiah
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
MSB Academy
 
Enhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
Types of keys dbms
Types of keys dbms
Surkhab Shelly
 
Sql select
Sql select
Mudasir Syed
 
Chapter14
Chapter14
SayantanLahiri4
 
4. tcp header.ppt
4. tcp header.ppt
SurajKumar732187
 
database concepts.pptx
database concepts.pptx
slavskrillex
 
Dbms keys
Dbms keys
RUpaliLohar
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
Unit 4 - Network Layer
Unit 4 - Network Layer
Chandan Gupta Bhagat
 
Intro to c++
Intro to c++
temkin abdlkader
 
Data Mining: Data cube computation and data generalization
Data Mining: Data cube computation and data generalization
DataminingTools Inc
 
10. XML in DBMS
10. XML in DBMS
koolkampus
 
PL SQL.pptx in computer language in database
PL SQL.pptx in computer language in database
ironman82715
 
PLSQL (1).ppt
PLSQL (1).ppt
MadhuriAnaparthy
 

More Related Content

What's hot (20)

Ppt of dbms e r features
Ppt of dbms e r features
Nirali Akabari
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints
Kumar
 
Entity Relationship Model
Entity Relationship Model
Slideshare
 
SQL Constraints
SQL Constraints
Randy Riness @ South Puget Sound Community College
 
DBMS unit-3.pdf
DBMS unit-3.pdf
Prof. Dr. K. Adisesha
 
Data Models.ppt
Data Models.ppt
AnshikaGoel42
 
Rehashing
Rehashing
rajshreemuthiah
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
MSB Academy
 
Enhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
Types of keys dbms
Types of keys dbms
Surkhab Shelly
 
Sql select
Sql select
Mudasir Syed
 
Chapter14
Chapter14
SayantanLahiri4
 
4. tcp header.ppt
4. tcp header.ppt
SurajKumar732187
 
database concepts.pptx
database concepts.pptx
slavskrillex
 
Dbms keys
Dbms keys
RUpaliLohar
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
Unit 4 - Network Layer
Unit 4 - Network Layer
Chandan Gupta Bhagat
 
Intro to c++
Intro to c++
temkin abdlkader
 
Data Mining: Data cube computation and data generalization
Data Mining: Data cube computation and data generalization
DataminingTools Inc
 
10. XML in DBMS
10. XML in DBMS
koolkampus
 
Ppt of dbms e r features
Ppt of dbms e r features
Nirali Akabari
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints
Kumar
 
Entity Relationship Model
Entity Relationship Model
Slideshare
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
MSB Academy
 
Enhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
database concepts.pptx
database concepts.pptx
slavskrillex
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
Data Mining: Data cube computation and data generalization
Data Mining: Data cube computation and data generalization
DataminingTools Inc
 
10. XML in DBMS
10. XML in DBMS
koolkampus
 

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

PL SQL.pptx in computer language in database
PL SQL.pptx in computer language in database
ironman82715
 
PLSQL (1).ppt
PLSQL (1).ppt
MadhuriAnaparthy
 
Dynamic websites lec3
Dynamic websites lec3
Belal Arfa
 
PLSQL.pptx
PLSQL.pptx
git21is061t
 
PL-SQL.pdf
PL-SQL.pdf
Anas Nakash
 
PLSQL Note
PLSQL Note
Arun Sial
 
SQL / PL
SQL / PL
srijanani2030
 
PLSQLmy Updated (1).pptx
PLSQLmy Updated (1).pptx
vamsiyadav39
 
PLSQL
PLSQL
Shubham Bammi
 
Plsql
Plsql
Mandeep Singh
 
L9 l10 server side programming
L9 l10 server side programming
Rushdi Shams
 
Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
Lecture Notes Unit5 chapter19 Cursor in Pl/SQL
Lecture Notes Unit5 chapter19 Cursor in Pl/SQL
Murugan146644
 
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PallaviGholap4
 
plsql.ppt
plsql.ppt
faizan992426
 
Module04
Module04
Sridhar P
 
Plsql
Plsql
fika sweety
 
Arduino Functions
Arduino Functions
mahalakshmimalini
 
Assignment#08
Assignment#08
Sunita Milind Dol
 
Ad

More from Murugan146644 (19)

Computer Network Unit IV - Lecture Notes - Network Layer
Computer Network Unit IV - Lecture Notes - Network Layer
Murugan146644
 
Assembly Language Program for 16_bit_Addition
Assembly Language Program for 16_bit_Addition
Murugan146644
 
Lecture Notes Microprocessor and Controller Unit2
Lecture Notes Microprocessor and Controller Unit2
Murugan146644
 
Lecture Notes Unit III PArt 2 - The DataLink Layer Medium Access Control Subl...
Lecture Notes Unit III PArt 2 - The DataLink Layer Medium Access Control Subl...
Murugan146644
 
Lecture Notes Unit III The DataLink Layer
Lecture Notes Unit III The DataLink Layer
Murugan146644
 
Lecture Notes - Microprocessor - Unit 1 - Microprocessor Architecture and its...
Lecture Notes - Microprocessor - Unit 1 - Microprocessor Architecture and its...
Murugan146644
 
Lecture Notes - Unit II - The Physical Layer
Lecture Notes - Unit II - The Physical Layer
Murugan146644
 
Lecture Notes - Introduction to Computer Network
Lecture Notes - Introduction to Computer Network
Murugan146644
 
Lecture Notes Unit3 chapter22 - distributed databases
Lecture Notes Unit3 chapter22 - distributed databases
Murugan146644
 
Lecture Notes Unit3 chapter21 - parallel databases
Lecture Notes Unit3 chapter21 - parallel databases
Murugan146644
 
Lecture Notes Unit3 chapter20 - Database System Architectures
Lecture Notes Unit3 chapter20 - Database System Architectures
Murugan146644
 
Relational Database Management System (RDBMS) LAB - GROUP B
Relational Database Management System (RDBMS) LAB - GROUP B
Murugan146644
 
Relational Database Managment System Lab - Group A
Relational Database Managment System Lab - Group A
Murugan146644
 
Lecture Notes Unit2 chapter7 RelationalDatabaseDesign
Lecture Notes Unit2 chapter7 RelationalDatabaseDesign
Murugan146644
 
Lecture Notes Unit 1 chapter 6 E-R MODEL
Lecture Notes Unit 1 chapter 6 E-R MODEL
Murugan146644
 
Lecture Notes Unit5 chapter18 Packages.pdf
Lecture Notes Unit5 chapter18 Packages.pdf
Murugan146644
 
Lecture Notes Unit5 chapter17 Stored procedures and functions
Lecture Notes Unit5 chapter17 Stored procedures and functions
Murugan146644
 
Lecture Notes Unit5 chapter16 Trigger Creation
Lecture Notes Unit5 chapter16 Trigger Creation
Murugan146644
 
Lecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privileges
Murugan146644
 
Computer Network Unit IV - Lecture Notes - Network Layer
Computer Network Unit IV - Lecture Notes - Network Layer
Murugan146644
 
Assembly Language Program for 16_bit_Addition
Assembly Language Program for 16_bit_Addition
Murugan146644
 
Lecture Notes Microprocessor and Controller Unit2
Lecture Notes Microprocessor and Controller Unit2
Murugan146644
 
Lecture Notes Unit III PArt 2 - The DataLink Layer Medium Access Control Subl...
Lecture Notes Unit III PArt 2 - The DataLink Layer Medium Access Control Subl...
Murugan146644
 
Lecture Notes Unit III The DataLink Layer
Lecture Notes Unit III The DataLink Layer
Murugan146644
 
Lecture Notes - Microprocessor - Unit 1 - Microprocessor Architecture and its...
Lecture Notes - Microprocessor - Unit 1 - Microprocessor Architecture and its...
Murugan146644
 
Lecture Notes - Unit II - The Physical Layer
Lecture Notes - Unit II - The Physical Layer
Murugan146644
 
Lecture Notes - Introduction to Computer Network
Lecture Notes - Introduction to Computer Network
Murugan146644
 
Lecture Notes Unit3 chapter22 - distributed databases
Lecture Notes Unit3 chapter22 - distributed databases
Murugan146644
 
Lecture Notes Unit3 chapter21 - parallel databases
Lecture Notes Unit3 chapter21 - parallel databases
Murugan146644
 
Lecture Notes Unit3 chapter20 - Database System Architectures
Lecture Notes Unit3 chapter20 - Database System Architectures
Murugan146644
 
Relational Database Management System (RDBMS) LAB - GROUP B
Relational Database Management System (RDBMS) LAB - GROUP B
Murugan146644
 
Relational Database Managment System Lab - Group A
Relational Database Managment System Lab - Group A
Murugan146644
 
Lecture Notes Unit2 chapter7 RelationalDatabaseDesign
Lecture Notes Unit2 chapter7 RelationalDatabaseDesign
Murugan146644
 
Lecture Notes Unit 1 chapter 6 E-R MODEL
Lecture Notes Unit 1 chapter 6 E-R MODEL
Murugan146644
 
Lecture Notes Unit5 chapter18 Packages.pdf
Lecture Notes Unit5 chapter18 Packages.pdf
Murugan146644
 
Lecture Notes Unit5 chapter17 Stored procedures and functions
Lecture Notes Unit5 chapter17 Stored procedures and functions
Murugan146644
 
Lecture Notes Unit5 chapter16 Trigger Creation
Lecture Notes Unit5 chapter16 Trigger Creation
Murugan146644
 
Lecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privileges
Murugan146644
 
Ad

Recently uploaded (20)

June 2025 Progress Update With Board Call_In process.pptx
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 

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: [email protected] 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.