1 What Are Block Types in PL - SQL
1 What Are Block Types in PL - SQL
Blocks
Generally a program written in PL/SQL language is divided into blocks. We can say
blocks are basic programming units in PL/SQL programming language.
PL/SQL Blocks contain set of instructions for oracle to execute, display information to the
screen, write data to file, call other programs, manipulate data and many more.
1. Anonymous Block
2. Named Block
Anonymous Block
As the title suggests these anonymous blocks do not have any names as a result they
cannot be stored in database and referenced later.
Named Block
On the other hand Named PL/SQL blocks are the one that have names and are used
when creating subroutines such as procedures, functions and packages. These
subroutines then can be stored in the database and referenced by their name later.
Both type of PL/SQL blocks are further divided into 3 different sections which are:
www.rebellionrider.com/block-types-in-pl-sql-by-rebellionrider-manish-sharma/#.VaJbOvmqqko 1/4
10/31/22, 9:00 PM What Are Block Types In PL/SQL | RebellionRider
The Execution Section is the only mandatory section of block whereas Declaration and
Exception Handling sections are optional.
DECLARE
Declaration Statements
BEGIN
Executable statements
Exception
Exception handling statements
END;
Declaration Section
This is the first section of PL/SQL block which contains definition of PL/SQL identifiers
such as variables, Constants, cursors and so on. You can say this is the place where all
local variables used in the program are defined and documented.
Example 1
DECLARE
Var_first_name VARCHAR2(30);
Var_last_name VARCHAR2(30);
Con_flag CONSTANT NUMBER:=0;
The above example shows declaration section of an anonymous block. It begins with
keyword declare and contains two variables var_first_name and var_last_name and one
constant con_flag. Notice that semicolon terminates each declaration.
Execution Section
This section contains executable statements that allow you to manipulate the variables
that have been declared in the declaration section. The content of this section must be
complete to allow the block to compile. By complete I mean complete set of instruction
for the PL/SQL engine must be between BEGIN and END keyword.
The execution Section of any PL/SQL block always begins with the Keyword BEGIN and
ends with the Keyword END.
This is the only mandatory section in PL/SQL block. This section supports all DML
commands and SQL*PLUS built-in functions and using Native Dynamic SQL (NDS) or
using DMBS_SQL built-in package it also supports DDL commands.
Example 2
www.rebellionrider.com/block-types-in-pl-sql-by-rebellionrider-manish-sharma/#.VaJbOvmqqko 2/4
10/31/22, 9:00 PM What Are Block Types In PL/SQL | RebellionRider
BEGIN
This is very simple program where I fetched the value of first name and last name
column from employees table where employee id is 100 and stored it into the variable
var_first_name and var_last_name which we declared in our first example.
Exception-Handling Section
This is the last section of PL/SQL block which is optional like the declaration block. This
section contains statements that are executed when a runtime error occurs within the
block.
Runtime error occurs while the program is running and cannot be detected by the
PL/SQL compiler. When a runtime error occurs, controlled is pass to the exception
handling section of the block the error is evaluated and specific exception is raised.
Example 3
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (‘No Employee
Found with ’||employee_id);
That’s it for the first tutorial you can also watch my video on the same topic where I
performed all the examples live. You can also tell me your feedback on my Twitter, for
that follow me and tell me how you like this tutorial. Also help me in reaching out to
more people by sharing this tutorial with your friends on social media. Thanks & have a
great day!
Manish Sharma
http://www.YouTube.com/RebellionRider
Manish Sharma, a recipient of the ORACLE ACE Award, is an Oracle database trainer & consultant. He
has an extremely successful YouTube channel named Rebellion Rider. It is one of the top ranking
channels when it comes to Oracle database tutorials with over 10 million views.
www.rebellionrider.com/block-types-in-pl-sql-by-rebellionrider-manish-sharma/#.VaJbOvmqqko 3/4