0% found this document useful (0 votes)
132 views3 pages

1 What Are Block Types in PL - SQL

There are two types of blocks in PL/SQL: anonymous blocks and named blocks. Anonymous blocks do not have names and cannot be stored in the database, while named blocks can be stored and referenced by name later. All PL/SQL blocks have three sections: declaration, execution, and exception handling. The execution section is mandatory and contains the executable statements, while the declaration and exception handling sections are optional. Blocks allow dividing PL/SQL code into logical units and declaring variables to be used within the block.
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)
132 views3 pages

1 What Are Block Types in PL - SQL

There are two types of blocks in PL/SQL: anonymous blocks and named blocks. Anonymous blocks do not have names and cannot be stored in the database, while named blocks can be stored and referenced by name later. All PL/SQL blocks have three sections: declaration, execution, and exception handling. The execution section is mandatory and contains the executable statements, while the declaration and exception handling sections are optional. Blocks allow dividing PL/SQL code into logical units and declaring variables to be used within the block.
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/ 3

10/31/22, 9:00 PM What Are Block Types In PL/SQL | RebellionRider

What Are Block Types In PL/SQL


By Manish Sharma - January 10, 2019

PL/SQL stands for procedural language-standard query language. It is a significant


member of Oracle programming tool set which is extensively used to code server side
programming. Similar to SQL language PL/SQL is also a case-insensitive programming
language.

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.

Does Blocks supports DDL statements?


Yes, PL/SQL blocks support all DML statements and using Native Dynamic SQL (NDS) or
they can run DDL statements using the build in DBMS_SQL package.

Types of PL/SQL Blocks


There are two types of blocks in PL/SQL

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:

1. The Declaration Section

2. The Execution Section and

3. The Exception-handling Section

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.

Basic prototype of Anonymous PL/SQL Block

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

SELECT first_name, last_name INTO var_first_name,


var_last_name
FROM employees WHERE employee_id =100;
DBMS_OUTPUT.PUT_LINE
(‘Employee Name ’||var_first_name||’ ‘||var_last_name);
END;

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

You might also like