Home » PL/SQL

Block Structure of PL/SQL in DBMS

In this tutorial, we are going to learn about the block structure of PL/SQL in the DBMS (database management system).
Submitted by Anushree Goswami, on July 01, 2019

PL/SQL is Block Structured. What is Block? PL/SQL programs are built on the basic units. These basic units are the blocks. Blocks can be named as Functions and Procedures or Anonymous Blocks as well. There is a different section of blocks:

  1. Header Section
  2. Declaration Section
  3. Executable Section
  4. Exception Section

This is how Block Structure works in PL/SQL...

    HEADER
    Type and Name of block 
    DECLARE 
    Variables; Constants; Cursors; 
    BEGIN 
    PL/SQL and SQL Statements
    EXCEPTION 
    Exception handlers
    END;

The blocks in PL/SQL are defined by the keywords DECLARE, BEGIN, EXCEPTION and END which divides the block into three sections:

  1. Declarative: Declarative statements are those statements that are used to declare variables, constants and other code elements, which can be further used in that block.
  2. Executable: Executable statements are those statements that are run when the block is executed.
  3. Exception Handling: Exception Handling is the special structured section that can be used to "catch" or trap any exceptions that are raised when the executable section runs.

To run this section, only the executable section is required. You don't have to declare anything in a block and you also don't have to catch an exception raised in that block.

It is an important point to be taken into count, a block is itself an executable statement, and so nested blocks can be applied in it.

For e.g. –

    DECLARE
    a number;
    text1 varchar2 (20);
    text2 varchar2 (20):= "HI";
    BEGIN
    ------------------------------
    END;

The important data types in PL/SQL are these:

NUMBER, INTEGER, CHAR, VARCHAR2, DATE etc.

To convert the string into the data: to_date('02-05-2007', 'dd-mm-yyyy')

E.g. 1- How to write a HELLO WORLD! Program in PL/SQL?

The HELLO WORLD! Program contains an executable section that invokes the procedure DBMS_OUTPUT.put_line to display the text on the screen.

Syntax:

BEGIN
DBMS_OUTPUT.put_line('HELLO WORLD!');
END;

E.g. 2- How to input E.g. 1 in a form of string in a PL/SQL program?

Here, block declares the variable of the type VARCHAR2 (string) whose maximum length is 100 bytes which can hold the string 'HELLO WORLD!' which is given as input by the user. DBMS_OUTPUT.put_line then accepts the variable and not the literal string in order to display it on the screen.

Syntax:

DECLARE
l_messageVARCHAR2 (100):='HELLO WORLD!';
BEGIN
DBMS_OUTPUT.put_line(l_message);
END;

E.g. 3- How to catch any exception if they might occur in E.g. 2?

Here, an exception section is added in order to catch the exception (WHEN OTHERS) that might be raised when an error is found in the program and displays an error message which is returned by the SQL (provided by Oracle).

Syntax:

DECLARE
l_messageVARCHAR2 (100):='HELLO WORLD!';
BEGIN
DBMS_OUTPUT.put_line(l_message);
EXCEPTIONWHENOTHERS
THEN
DBMS_OUTPUT.put_line(SQL);
END;

E.g. 4- How to nest the blocks as well as join together multiple strings in E.g. 3?

Here, In order to nest the blocks, two variable of the type VARCHAR2 (string) whose maximum length is 100 bytes are needed to be declared and a concatenation (||) operator is used to join together multiple strings.

Syntax:

DECLARE 
l_messageVARCHAR2 (100):= 'HELLO'; 
BEGIN 
DECLARE 
l_message2 VARCHAR2 (100):= l_message||' WORLD!';
BEGIN 
DBMS_OUTPUT.put_line(l_message2); 
END; 
EXCEPTION WHEN OTHERS 
THEN DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack); 
END;

There are various tools to execute PL/SQL code. The most basic and popular one is SQL*Plus. It is a command-line interface and is used to execute SQL statements as well as PL/SQL blocks.

When some developers are solely dependent on SQL*Plus there are many others who use different IDE (Integrated Development Environment) for the same. The most popular IDE's are:

  1. Oracle SQL Developer, from Oracle
  2. Toad and SQL Navigator, from Quest Software
  3. PL/SQL Developer, from AllroundAutomations





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.