0% found this document useful (0 votes)
0 views

PL SQL Introduction

PL/SQL is a block-structured language developed by Oracle that combines SQL with procedural programming, allowing for efficient data manipulation and control-flow logic within the Oracle Database. The document covers PL/SQL basics, features, differences from SQL, block structure, and practical examples, emphasizing its capabilities for creating reusable program units and handling exceptions. It concludes that PL/SQL enhances SQL's data manipulation abilities, enabling developers to create sophisticated database applications.

Uploaded by

plcprasad123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

PL SQL Introduction

PL/SQL is a block-structured language developed by Oracle that combines SQL with procedural programming, allowing for efficient data manipulation and control-flow logic within the Oracle Database. The document covers PL/SQL basics, features, differences from SQL, block structure, and practical examples, emphasizing its capabilities for creating reusable program units and handling exceptions. It concludes that PL/SQL enhances SQL's data manipulation abilities, enabling developers to create sophisticated database applications.

Uploaded by

plcprasad123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Databases SQL MySQL PostgreSQL PL/SQL MongoDB SQL Cheat Sheet SQL Interview

PL/SQL Introduction
Last Updated : 11 Nov, 2024

PL/SQL (Procedural Language/Structured Query Language) is a block-


structured language developed by Oracle that allows developers to
combine the power of SQL with procedural programming constructs.
The PL/SQL language enables efficient data manipulation and control-
flow logic, all within the Oracle Database.

In this article, we’ll cover PL/SQL basics, including its core features,
PL/SQL block structure, and practical examples that demonstrate the
power of PL/SQL. We’ll also explore the differences between SQL and
PL/SQL, how variables and identifiers work, and how the PL/SQL
execution environment operates within Oracle.

Basics of PL/SQL
PL/SQL stands for Procedural Language extensions to the
Structured Query Language (SQL).
PL/SQL is a combination of SQL along with the procedural features
of programming languages.
Oracle uses a PL/SQL engine to process the PL/SQL statements.
PL/SQL includes procedural language elements like conditions and
loops. It allows declaration of constants and variables, procedures
and functions, types and variable of those types and triggers.

Features of PL/SQL
1. PL/SQL is basically a procedural language, which provides the
functionality of decision-making, iteration, and many more features
of procedural programming languages.
2. PL/SQL can execute a number of queries in one block using single
command.
Open In App
3. One can create a PL/SQL unit such as procedures, functions,
packages, triggers, and types, which are stored in the database for
reuse by applications.
4. PL/SQL provides a feature to handle the exception which occurs in
PL/SQL block known as exception handling block.
5. Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
6. PL/SQL Offers extensive error checking.

Differences Between SQL and PL/SQL

SQL PL/SQL

SQL is a single query that is used PL/SQL is a block of codes that


to perform DML and DDL used to write the entire program
operations. blocks/ procedure/ function, etc.

It is declarative, that defines what


PL/SQL is procedural that defines
needs to be done, rather than how
how the things needs to be done.
things need to be done.

Execute as a single statement. Execute as a whole block.

Mainly used to create an


Mainly used to manipulate data.
application.

It is an extension of SQL, so it can


Cannot contain PL/SQL code in it.
contain SQL inside it.

Structure of PL/SQL Block


PL/SQL extends SQL by adding constructs found in procedural
languages, resulting in a structural language that is more powerful than
SQL. The basic unit in PL/SQL is a block. All PL/SQL programs are
made up of blocks, which can be nested within each other.
Open In App
Typically, each block performs a logical action in the program. A block
has the following structure:

DECLARE
declaration statements;

BEGIN
executable statements

EXCEPTIONS
exception handling statements

END;
Open In App
Declare section starts with DECLARE keyword in which variables,
constants, records as cursors can be declared which stores data
temporarily. It basically consists definition of PL/SQL identifiers.
This part of the code is optional.
Execution section starts with BEGIN and ends with END
keyword.This is a mandatory section and here the program logic is
written to perform any task like loops and conditional statements. It
supports all DML commands, DDL commands and SQL*PLUS built-in
functions as well.
Exception section starts with EXCEPTION keyword.This section is
optional which contains statements that are executed when a run-
time error occurs. Any exceptions can be handled in this section.

PL/SQL Identifiers

There are several PL/SQL identifiers such as variables, constants,


procedures, cursors, triggers etc.

1. Variables: Like several other programming languages, variables in


PL/SQL must be declared prior to its use. They should have a valid
name and data type as well. Syntax for declaration of variables:

variable_name datatype [NOT NULL := value ];

1. Example to show how to declare variables in PL/SQL :

SQL> SET SERVEROUTPUT ON;

SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;

BEGIN
null;
END;
/
Open In App
1. Output:

PL/SQL procedure successfully completed.

1. Explanation:
SET SERVEROUTPUT ON: It is used to display the buffer used by
the dbms_output.
var1 INTEGER : It is the declaration of variable, named var1 which
is of integer type. There are many other data types that can be
used like float, int, real, smallint, long etc. It also supports
variables used in SQL as well like NUMBER(prec, scale), varchar,
varchar2 etc.
PL/SQL procedure successfully completed.: It is displayed when
the code is compiled and executed successfully.
Slash (/) after END;: The slash (/) tells the SQL*Plus to execute
the block.
Assignment operator (:=) : It is used to assign a value to a
variable.

2. Displaying Output: The outputs are displayed by using


DBMS_OUTPUT which is a built-in package that enables the user to
display output, debugging information, and send messages from
PL/SQL blocks, subprograms, packages, and triggers. Let us see an
example to see how to display a message using PL/SQL :

SQL> SET SERVEROUTPUT ON;


SQL> DECLARE
var varchar2(40) := 'I love GeeksForGeeks' ;

BEGIN
dbms_output.put_line(var);

END;
/

1. Output:
Open In App
I love GeeksForGeeks

PL/SQL procedure successfully completed.

1. Explanation:
dbms_output.put_line : This command is used to direct the
PL/SQL output to a screen.

2. Using Comments: Like in many other programming languages, in


PL/SQL also, comments can be put within the code which has no
effect in the code. There are two syntaxes to create comments in
PL/SQL :
Single Line Comment: To create a single line comment , the
symbol – – is used.
Multi Line Comment: To create comments that span over several
lines, the symbol /* and */ is used.

3. Taking input from user: Just like in other programming languages, in


PL/SQL also, we can take input from the user and store it in a
variable. Let us see an example to show how to take input from
users in PL/SQL:

SQL> SET SERVEROUTPUT ON;

SQL> DECLARE

-- taking input for variable a


a number := &a;

-- taking input for variable b


b varchar2(30) := &b;

BEGIN
null;

END;
/
Open In App
1. Output:

Enter value for a: 24


old 2: a number := &a;
new 2: a number := 24;
Enter value for b: 'GeeksForGeeks'
old 3: b varchar2(30) := &b;
new 3: b varchar2(30) := 'GeeksForGeeks';

PL/SQL procedure successfully completed.

1. (***) Let us see an example on PL/SQL to demonstrate all above


concepts in one single block of code.

--PL/SQL code to print sum of two numbers taken from the


user.
SQL> SET SERVEROUTPUT ON;

SQL> DECLARE

-- taking input for variable a


a integer := &a ;

-- taking input for variable b


b integer := &b ;
c integer ;

BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||' is =
'||c);

END;
/

Enter value for a: 2


Enter value for b: 3

Sum of 2 and 3 is = 5 Open In App


PL/SQL procedure successfully completed.

PL/SQL Execution Environment:

The PL/SQL engine resides in the Oracle engine.The Oracle engine can
process not only single SQL statement but also block of many
statements.The call to Oracle engine needs to be made only once to
execute any number of SQL statements if these SQL statements are
bundled inside a PL/SQL block.

Conclusion
PL/SQL is a powerful tool in Oracle for combining SQL with procedural
programming capabilities. With PL/SQL features like error handling,
reusable program units, and support for loops and conditionals,
PL/SQL extends SQL’s data manipulation capabilities and enables
developers to create sophisticated applications within the database. By
understanding SQL vs PL/SQL and the advantages of the PL/SQL
execution environment, developers can unlock the full potential of
Oracle’s PL/SQL language for robust database applications.

Dreaming of M.Tech in IIT? Get AIR under 100 with our GATE 2026
CSE & DA courses! Get flexible weekday/weekend options, live
mentorship, and mock tests. Access exclusive features like All India
Mock Tests, and Doubt Solving—your GATE success starts now!

Comment More info


Next Article

Advertise with us
PL/SQL Injection

Similar Reads

Introduction to SQLite
SQLite is a highly efficient, serverless, and self-contained SQL database
Open In App
engine that stands out for its simplicity and ease of integration. Designe…
9 min read

Introduction to Couchbase
Couchbase Server is an open-source, distributed, multi-model NoSQL,
JSON document database that is enhanced for interactive applications. …

3 min read

Introduction to Apache CouchDB


Apache CouchDB was developed by Apache Software Foundation and
initially released in 2005. CouchDB is written in Erlang. It is an open-…

3 min read

PostgreSQL - Introduction to Stored Procedures


PostgreSQL allows the users to extend the database functionality with
the help of user-defined functions and stored procedures through variou…

5 min read

Introduction to Transaction Processing


Single user system : In this at-most, only one user at a time can use the
system. Multi-user system : In the same, many users can access the…

2 min read

Introduction to PostgreSQL PL/pgSQL


PostgreSQL is an open-source, strong and highly extensible object-
relational database system. It combines the power of SQL with addition…

4 min read

Introduction of Shared Memory Segment


Introduction of Shared Memory Segment :The quickest kind of IPC
accessible is shared memory. There is no kernel participation in…

6 min read

Open In App
JSON Introduction
JSON (JavaScript Object Notation) is a lightweight, text-based data
format used for representing structured data. It is one of the most widel…

5 min read

Introduction to Graph Database on NoSQL


A graph database is a type of NoSQL database that is designed to handle
data with complex relationships and interconnections. In a graph…

8 min read

Introduction to NoSQL Cloud Database Services


NoSQL Cloud Database Services are cloud-based database services that
provide scalable, high-performance, and cost-effective solutions for…

6 min read

Corporate & Communications


Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar
Pradesh (201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida,
Gautam Buddh Nagar, Uttar Pradesh,
201305

Advertise with us

Open In App

You might also like