SlideShare a Scribd company logo
PL/SQL : INTRODUCTION
Ms.Sujaya S
Assistant Professor
Department of CSE
KIT-Kalaignarkarunanidhi Institute of Technology
PL/SQL
• PL/SQL is Oracle's procedural language extension
to SQL, the non-procedural relational database
language.
• With PL/SQL, you can use SQL statements to
manipulate ORACLE data and the flow of control
statements to process the data. Moreover, you can
declare constants and variables, define
subprograms (procedures and functions), and trap
runtime errors. Thus, PL/SQL combines the data
manipulating power of SQL with the data
processing power of procedural languages.
PL/SQL
• Many Oracle applications are built using client-server
architecture. The Oracle database resides on the server.
• The program that makes requests against this database
resides on the client machine.
• This program can be written in C, Java, or PL/SQL.
• While PL/SQL is just like any other programming
language, it has syntax and rules that determine how
programming statements work together. It is important
for you to realize that PL/SQL is not a stand-alone
programming language.
• PL/SQL is a part of the Oracle RDBMS, and it can reside
in two environments, the client and the server.
PL/SQL
• As a result, it is very easy to move PL/SQL
modules between server-side and client-side
applications.
• When the PL/SQL engine is located on the
server, the whole PL/SQL block is passed to the
PL/SQL engine on the Oracle server.
• The PL/SQL engine processes the block
according to the Figure 2.1.
Introduction to PLSQL.PPT
PL/SQL
• When the PL/SQL engine is located on the client,
as it is in the Oracle Developer Tools, the
PL/SQL processing is done on the client side.
• All SQL statements that are embedded within
the PL/SQL block are sent to the Oracle server
for further processing. When PL/SQL block
contains no SQL statement, the entire block is
executed on the client side.
DIFFERENCE BETWEEN PL/SQL AND SQL
• When a SQL statement is issued on the client
computer, the request is made to the database on
the server, and the result set is sent back to the
client.
• As a result, a single SQL statement causes two trips
on the network. If multiple SELECT statements are
issued, the network traffic increase significantly very
fast. For example, four SELECT statements cause
eight network trips.
• If these statements are part of the PL/SQL block, they
are sent to the server as a single unit. The SQL
statements in this PL/SQL program are executed at
the server and the result set is sent back as a single
unit. There is still only one network trip made as is in
case of a single SELECT statement.
Comparison of SQL*PLUS and PL/SQL
PL/SQL BLOCKS
• PL/SQL blocks can be divided into two groups:
1. Named and
2. Anonymous.
• Named blocks are used when creating subroutines.
These subroutines are procedures, functions, and
packages.
• The subroutines can be stored in the database and
referenced by their names later on.
• In addition, subroutines can be defined within the
anonymous PL/SQL block.
• Anonymous PL/SQL blocks do not have names. As a
result,they cannot be stored in the database and
referenced later.
PL/SQL BLOCK STRUCTURE
• PL/SQL blocks contain three sections
1. Declare section
2. Executable section and
3. Exception-handling section.
• The executable section is the only mandatory
section of the block.
• Both the declaration and exception-handling
sections are optional.
PL/SQL BLOCK STRUCTURE
• PL/SQL block has the following structure:
DECLARE
Declaration statements
BEGIN
Executable statements
EXCETION
Exception-handling statements
END ;
DECLARATION SECTION
• The declaration section is the first section of
the PL/SQL block.
• It contains definitions of PL/SQL identifiers
such as variables, constants, cursors and so on.
• Example
DECLARE
v_first_name VARCHAR2(35) ;
v_last_name VARCHAR2(35) ;
v_counter NUMBER := 0 ;
EXECUTABLE SECTION
• The executable section is the next section of the
PL/SQL block.
• This section contains executable statements that allow
you to manipulate the variables that have been
declared in the declaration section.
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123 ;
DBMS_OUTPUT.PUT_LINE
(‘Student name :’ || v_first_name ||‘ ’|| v_last_name);
END;
EXCEPTION-HANDLING SECTION
• The exception-handling section is the last
section of the PL/SQL block.
• This section contains statements that are
executed when a runtime error occurs within a
block.
• Runtime errors occur while the program is
running and cannot be detected by the PL/SQL
compiler.
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
(‘ There is no student with student id 123 ’);
END;
HOW PL/SQL GETS EXECUTED
• Every time an anonymous block is executed, the
code is sent to the PL/SQL engine on the server
where it is compiled.
• The named PL/SQL block is compiled only at the
time of its creation, or if it has been changed.
• The compilation process includes syntax checking,
binding and p-code generation.
• Syntax checking involves checking PL/SQL code for
syntax or compilation errors.
• Once the programmer corrects syntax errors, the
compiler can assign a storage address to program
variables that are used to hold data for Oracle. This
process is called Binding.
HOW PL/SQL GETS EXECUTED
• After binding, p-code is generated for the
PL/SQL block.
• P-code is a list of instructions to the PL/SQL
engine.
• For named blocks, p-code is stored in the
database, and it is used the next time the
program is executed.
• Once the process of compilation has completed
successfully, the status for a named PL/SQL
block is set to VALID, and also stored in the
database.
• If the compilation process was not successful,
the status for a named PL/SQL block is set to
INVALID.
PL/SQL IN SQL*PLUS
• SQL*Plus is an interactive tool that allows
you to type SQL or PL/SQL statements at
the command prompt.
• These statements are then sent to the
database. Once they are processed, the
results are sent back from the database
and displayed on the screen.
• There are some differences between
entering SQL and PL/SQL statements.
SQL EXAMPLE
SELECT first_name, last_name
FROM student;
• The semicolon terminates this SELECT
statement. Therefore, as soon as you type
semicolon and hit the ENTER key, the
result set is displayed to you.
PL/SQL EXAMPLE
DECLARE
v_first_name VARCHAR2(35);
v_last_name VARCHAR2(35);
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123;
DBMS_OUTPUT.PUT_LINE
('Student name: '||v_first_name||' '||v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
('There is no student with student id 123');
END;
.
/
PL/SQL EXAMPLE
• There are two additional lines at the end of the
block containing “.” and “/”. The “.” marks the end of
the PL/SQL block and is optional.
• The “/” executes the PL/SQL block and is required.
• When SQL*Plus reads SQL statement, it knows that
the semicolon marks the end of the statement.
Therefore, the statement is complete and can be
sent to the database.
• When SQL*Plus reads a PL/SQL block, a semicolon
• marks the end of the individual statement within
the block. In other words, it is not a block
terminator.
PL/SQL EXAMPLE
• Therefore, SQL*Plus needs to know when the
block has ended. As you have seen in the
example, it can be done with period and
forward slash.
EXECUTING PL/SQL
PL/SQL can be executed directly in SQL*Plus.
A PL/SQL program is normally saved with
an .sql extension. To execute an
anonymous PL/SQL program, simply type
the following command at the SQL
prompt:
SQL> @DisplayAge
GENERATING OUTPUT
Like other programming languages, PL/SQL provides a
procedure (i.e. PUT_LINE) to allow the user to
display the output on the screen. For a user to able
to view a result on the screen, two steps are
required.
First, before executing any PL/SQL program, type the
following command at the SQL prompt (Note: you
need to type in this command only once for every
SQL*PLUS session):
SQL> SET SERVEROUTPUT ON;
or put the command at the beginning of the program,
right before the declaration section.
GENERATING OUTPUT
Second, use DBMS_OUTPUT.PUT_LINE in your
executable section to display any message you
want to the screen.
Syntax for displaying a message:
DBMS_OUTPUT.PUT_LINE(<string>);
in which PUT_LINE is the procedure to generate the
output on the screen, and DBMS_OUTPUT is the
package to which the PUT_LINE belongs.
DBMS_OUTPUT_PUT_LINE(‘My age is ‘ ||
num_age);
SUBSTITUTIONVARIABLES
• SQL*Plus allows a PL/SQL block to receive input
information with the help of substitution
variables.
• Substitution variables cannot be used to output
the values because no memory is allocated for
them.
• SQL*Plus will substitute a variable before the
PL/SQL block is sent to the database.
• Substitution variables are usually prefixed by the
ampersand(&) character or double ampersand
(&&) character.
EXAMPLE
DECLARE
v_student_id NUMBER := &sv_student_id;
v_first_name VARCHAR2(35);
v_last_name VARCHAR2(35);
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = v_student_id;
DBMS_OUTPUT.PUT_LINE
('Student name: '||v_first_name||' '||v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('There is no such student');
END;
EXAMPLE
• When this example is executed, the user is asked
to provide a value for the student ID.
• The example shown above uses a single
ampersand for the substitution variable.
• When a single ampersand is used throughout
the PL/SQL block, the user is asked to provide a
value for each occurrence of the substitution
variable.
EXAMPLE
BEGIN
DBMS_OUTPUT.PUT_LINE('Today is ‘||’&sv_day');
DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
END;
This example produces the following output:
Enter value for sv_day: Monday
old 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ &sv_day');
new 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ Monday');
Enter value for sv_day: Tuesday
old 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
new 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ Tuesday');
Today is Monday
Tomorrow will be Tuesday
PL/SQL procedure successfully completed.
EXAMPLE
• When a substitution variable is used in the
script, the output produced by the program
contains the statements that show how the
substitution was done.
• If you do not want to see these lines displayed
in the output produced by the script, use the
SET command option before you run the script
as shown below:
SET VERIFY OFF;
EXAMPLE
• Then, the output changes as shown below:
Enter value for sv_day: Monday
Enter value for sv_day: Tuesday
Today is Monday
Tomorrow will be Tuesday
PL/SQL procedure successfully completed.
• The substitution variable sv_day appears twice in this PL/SQL block. As a
result, when this example is run, the user is asked twice to provide the value
for the same variable.
EXAMPLE
BEGIN
DBMS_OUTPUT.PUT_LINE('Today is '||'&&sv_day');
DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
END;
• In this example, substitution variable sv_day is
prefixed by double ampersand in the first
DBMS_OUTPUT.PUT_LINE statement. As a result,
this version of the example produces different
output.
OUTPUT
Enter value for sv_day: Monday
old 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ &&sv_day');
new 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ Monday');
old 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
new 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ Monday');
Today is Monday
Tomorrow will be Monday
PL/SQL procedure successfully completed.
• It is clear that the user is asked only once to provide the value for
the substitution variable sv_day.
• As a result, both DBMS_OUTPUT.PUT_LINE statements use the
value of Monday entered previously by the user.
Substitution Variables
 Ampersand(&) character and double ampersand
(&&) characters are the default characters that
denote substitution variables.
 There is a special SET command option available in
SQL*Plus that allows to change the default character
(&) to any other character or disable the substitution
variable feature.
 This SET command has the following syntax:
SET DEFINE character
or
SET DEFINE ON
or
SET DEFINE OFF
Substitution Variables
 The first set command option changes the prefix
of the substitution variable from an ampersand
to another character. This character cannot be
alphanumeric or white space.
 The second (ON option) and third (OFF option)
control whether SQL*Plus will look for
substitution variables or not.
 In addition, ON option changes the value of the
character back to the ampersand.
Ad

More Related Content

Similar to Introduction to PLSQL.PPT (20)

PL SQL Introduction- Blocks&example.pptx
PL SQL Introduction- Blocks&example.pptxPL SQL Introduction- Blocks&example.pptx
PL SQL Introduction- Blocks&example.pptx
sivamathi12
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
Danish Mehraj
 
pl_sql.ppt
pl_sql.pptpl_sql.ppt
pl_sql.ppt
Prabhat106214
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
Kailash N
 
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
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
pooja_123
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
Vaibhav Kathuria
 
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
Noorien3
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
Vinay Kumar
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
PrabhatKumar591
 
Plsql
PlsqlPlsql
Plsql
fika sweety
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
srijanani2030
 
Exploring plsql new features best practices september 2013
Exploring plsql new features best practices   september 2013Exploring plsql new features best practices   september 2013
Exploring plsql new features best practices september 2013
Andrejs Vorobjovs
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
Khadija Parween
 
Dynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptxDynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptx
angelinjeba6
 
PL SQL Introduction- Blocks&example.pptx
PL SQL Introduction- Blocks&example.pptxPL SQL Introduction- Blocks&example.pptx
PL SQL Introduction- Blocks&example.pptx
sivamathi12
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
Danish Mehraj
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
Kailash N
 
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
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
Vaibhav Kathuria
 
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
10gplsqlslide-120704232925-phJKKJJKKJpapp01.ppt
Noorien3
 
Exploring plsql new features best practices september 2013
Exploring plsql new features best practices   september 2013Exploring plsql new features best practices   september 2013
Exploring plsql new features best practices september 2013
Andrejs Vorobjovs
 
Dynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptxDynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptx
angelinjeba6
 

Recently uploaded (20)

2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Ad

Introduction to PLSQL.PPT

  • 1. PL/SQL : INTRODUCTION Ms.Sujaya S Assistant Professor Department of CSE KIT-Kalaignarkarunanidhi Institute of Technology
  • 2. PL/SQL • PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. • With PL/SQL, you can use SQL statements to manipulate ORACLE data and the flow of control statements to process the data. Moreover, you can declare constants and variables, define subprograms (procedures and functions), and trap runtime errors. Thus, PL/SQL combines the data manipulating power of SQL with the data processing power of procedural languages.
  • 3. PL/SQL • Many Oracle applications are built using client-server architecture. The Oracle database resides on the server. • The program that makes requests against this database resides on the client machine. • This program can be written in C, Java, or PL/SQL. • While PL/SQL is just like any other programming language, it has syntax and rules that determine how programming statements work together. It is important for you to realize that PL/SQL is not a stand-alone programming language. • PL/SQL is a part of the Oracle RDBMS, and it can reside in two environments, the client and the server.
  • 4. PL/SQL • As a result, it is very easy to move PL/SQL modules between server-side and client-side applications. • When the PL/SQL engine is located on the server, the whole PL/SQL block is passed to the PL/SQL engine on the Oracle server. • The PL/SQL engine processes the block according to the Figure 2.1.
  • 6. PL/SQL • When the PL/SQL engine is located on the client, as it is in the Oracle Developer Tools, the PL/SQL processing is done on the client side. • All SQL statements that are embedded within the PL/SQL block are sent to the Oracle server for further processing. When PL/SQL block contains no SQL statement, the entire block is executed on the client side.
  • 7. DIFFERENCE BETWEEN PL/SQL AND SQL • When a SQL statement is issued on the client computer, the request is made to the database on the server, and the result set is sent back to the client. • As a result, a single SQL statement causes two trips on the network. If multiple SELECT statements are issued, the network traffic increase significantly very fast. For example, four SELECT statements cause eight network trips. • If these statements are part of the PL/SQL block, they are sent to the server as a single unit. The SQL statements in this PL/SQL program are executed at the server and the result set is sent back as a single unit. There is still only one network trip made as is in case of a single SELECT statement.
  • 9. PL/SQL BLOCKS • PL/SQL blocks can be divided into two groups: 1. Named and 2. Anonymous. • Named blocks are used when creating subroutines. These subroutines are procedures, functions, and packages. • The subroutines can be stored in the database and referenced by their names later on. • In addition, subroutines can be defined within the anonymous PL/SQL block. • Anonymous PL/SQL blocks do not have names. As a result,they cannot be stored in the database and referenced later.
  • 10. PL/SQL BLOCK STRUCTURE • PL/SQL blocks contain three sections 1. Declare section 2. Executable section and 3. Exception-handling section. • The executable section is the only mandatory section of the block. • Both the declaration and exception-handling sections are optional.
  • 11. PL/SQL BLOCK STRUCTURE • PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCETION Exception-handling statements END ;
  • 12. DECLARATION SECTION • The declaration section is the first section of the PL/SQL block. • It contains definitions of PL/SQL identifiers such as variables, constants, cursors and so on. • Example DECLARE v_first_name VARCHAR2(35) ; v_last_name VARCHAR2(35) ; v_counter NUMBER := 0 ;
  • 13. EXECUTABLE SECTION • The executable section is the next section of the PL/SQL block. • This section contains executable statements that allow you to manipulate the variables that have been declared in the declaration section. BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123 ; DBMS_OUTPUT.PUT_LINE (‘Student name :’ || v_first_name ||‘ ’|| v_last_name); END;
  • 14. EXCEPTION-HANDLING SECTION • The exception-handling section is the last section of the PL/SQL block. • This section contains statements that are executed when a runtime error occurs within a block. • Runtime errors occur while the program is running and cannot be detected by the PL/SQL compiler. EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (‘ There is no student with student id 123 ’); END;
  • 15. HOW PL/SQL GETS EXECUTED • Every time an anonymous block is executed, the code is sent to the PL/SQL engine on the server where it is compiled. • The named PL/SQL block is compiled only at the time of its creation, or if it has been changed. • The compilation process includes syntax checking, binding and p-code generation. • Syntax checking involves checking PL/SQL code for syntax or compilation errors. • Once the programmer corrects syntax errors, the compiler can assign a storage address to program variables that are used to hold data for Oracle. This process is called Binding.
  • 16. HOW PL/SQL GETS EXECUTED • After binding, p-code is generated for the PL/SQL block. • P-code is a list of instructions to the PL/SQL engine. • For named blocks, p-code is stored in the database, and it is used the next time the program is executed. • Once the process of compilation has completed successfully, the status for a named PL/SQL block is set to VALID, and also stored in the database. • If the compilation process was not successful, the status for a named PL/SQL block is set to INVALID.
  • 17. PL/SQL IN SQL*PLUS • SQL*Plus is an interactive tool that allows you to type SQL or PL/SQL statements at the command prompt. • These statements are then sent to the database. Once they are processed, the results are sent back from the database and displayed on the screen. • There are some differences between entering SQL and PL/SQL statements.
  • 18. SQL EXAMPLE SELECT first_name, last_name FROM student; • The semicolon terminates this SELECT statement. Therefore, as soon as you type semicolon and hit the ENTER key, the result set is displayed to you.
  • 19. PL/SQL EXAMPLE DECLARE v_first_name VARCHAR2(35); v_last_name VARCHAR2(35); BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123; DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('There is no student with student id 123'); END; . /
  • 20. PL/SQL EXAMPLE • There are two additional lines at the end of the block containing “.” and “/”. The “.” marks the end of the PL/SQL block and is optional. • The “/” executes the PL/SQL block and is required. • When SQL*Plus reads SQL statement, it knows that the semicolon marks the end of the statement. Therefore, the statement is complete and can be sent to the database. • When SQL*Plus reads a PL/SQL block, a semicolon • marks the end of the individual statement within the block. In other words, it is not a block terminator.
  • 21. PL/SQL EXAMPLE • Therefore, SQL*Plus needs to know when the block has ended. As you have seen in the example, it can be done with period and forward slash.
  • 22. EXECUTING PL/SQL PL/SQL can be executed directly in SQL*Plus. A PL/SQL program is normally saved with an .sql extension. To execute an anonymous PL/SQL program, simply type the following command at the SQL prompt: SQL> @DisplayAge
  • 23. GENERATING OUTPUT Like other programming languages, PL/SQL provides a procedure (i.e. PUT_LINE) to allow the user to display the output on the screen. For a user to able to view a result on the screen, two steps are required. First, before executing any PL/SQL program, type the following command at the SQL prompt (Note: you need to type in this command only once for every SQL*PLUS session): SQL> SET SERVEROUTPUT ON; or put the command at the beginning of the program, right before the declaration section.
  • 24. GENERATING OUTPUT Second, use DBMS_OUTPUT.PUT_LINE in your executable section to display any message you want to the screen. Syntax for displaying a message: DBMS_OUTPUT.PUT_LINE(<string>); in which PUT_LINE is the procedure to generate the output on the screen, and DBMS_OUTPUT is the package to which the PUT_LINE belongs. DBMS_OUTPUT_PUT_LINE(‘My age is ‘ || num_age);
  • 25. SUBSTITUTIONVARIABLES • SQL*Plus allows a PL/SQL block to receive input information with the help of substitution variables. • Substitution variables cannot be used to output the values because no memory is allocated for them. • SQL*Plus will substitute a variable before the PL/SQL block is sent to the database. • Substitution variables are usually prefixed by the ampersand(&) character or double ampersand (&&) character.
  • 26. EXAMPLE DECLARE v_student_id NUMBER := &sv_student_id; v_first_name VARCHAR2(35); v_last_name VARCHAR2(35); BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = v_student_id; DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('There is no such student'); END;
  • 27. EXAMPLE • When this example is executed, the user is asked to provide a value for the student ID. • The example shown above uses a single ampersand for the substitution variable. • When a single ampersand is used throughout the PL/SQL block, the user is asked to provide a value for each occurrence of the substitution variable.
  • 28. EXAMPLE BEGIN DBMS_OUTPUT.PUT_LINE('Today is ‘||’&sv_day'); DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day'); END; This example produces the following output: Enter value for sv_day: Monday old 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ &sv_day'); new 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ Monday'); Enter value for sv_day: Tuesday old 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day'); new 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ Tuesday'); Today is Monday Tomorrow will be Tuesday PL/SQL procedure successfully completed.
  • 29. EXAMPLE • When a substitution variable is used in the script, the output produced by the program contains the statements that show how the substitution was done. • If you do not want to see these lines displayed in the output produced by the script, use the SET command option before you run the script as shown below: SET VERIFY OFF;
  • 30. EXAMPLE • Then, the output changes as shown below: Enter value for sv_day: Monday Enter value for sv_day: Tuesday Today is Monday Tomorrow will be Tuesday PL/SQL procedure successfully completed. • The substitution variable sv_day appears twice in this PL/SQL block. As a result, when this example is run, the user is asked twice to provide the value for the same variable.
  • 31. EXAMPLE BEGIN DBMS_OUTPUT.PUT_LINE('Today is '||'&&sv_day'); DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day'); END; • In this example, substitution variable sv_day is prefixed by double ampersand in the first DBMS_OUTPUT.PUT_LINE statement. As a result, this version of the example produces different output.
  • 32. OUTPUT Enter value for sv_day: Monday old 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ &&sv_day'); new 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ Monday'); old 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day'); new 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ Monday'); Today is Monday Tomorrow will be Monday PL/SQL procedure successfully completed. • It is clear that the user is asked only once to provide the value for the substitution variable sv_day. • As a result, both DBMS_OUTPUT.PUT_LINE statements use the value of Monday entered previously by the user.
  • 33. Substitution Variables  Ampersand(&) character and double ampersand (&&) characters are the default characters that denote substitution variables.  There is a special SET command option available in SQL*Plus that allows to change the default character (&) to any other character or disable the substitution variable feature.  This SET command has the following syntax: SET DEFINE character or SET DEFINE ON or SET DEFINE OFF
  • 34. Substitution Variables  The first set command option changes the prefix of the substitution variable from an ampersand to another character. This character cannot be alphanumeric or white space.  The second (ON option) and third (OFF option) control whether SQL*Plus will look for substitution variables or not.  In addition, ON option changes the value of the character back to the ampersand.