PL/SQL Block

PL/SQL block has 3 sections:

  1. Declare
  2. Begin
  3. Exception

Followed by END statement.

PL/SQL Block

PL/SQL blocks consist of codes that describes the process that has to be executed. Blocks consists, both, SQL statements, as well as PL/SQL instructions.

DECLARE Section:

Cursors, triggers and other oracle objects are declared in this section. These declared variables can be used in SQL statements for data manipulation.

BEGIN Section:

This section contains the main logic which is responsible for data retrieval and manipulation.

EXCEPTION Section:

This section handles the errors that might occur between BEGIN and EXCEPTION

END Section:

This section indicates the end of a PL/SQL block.

Basic Examples:

  1. The first example doesn’t have a DECLARE section:
BEGIN
   NULL;
END;
BEGIN
   DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
/

OUTPUT
Hello World!
  1. In this example, we will DECLARE 2 variables and print their addition:
DECLARE 
    -- declare variable a and b  
    -- and these variables have integer datatype  
   a number;  
   b number;   
BEGIN 
   a:= 7;  
   b:= 77;  
   dbms_output.put_line('Sum of the number is: ' || a + b);  
END;  
/

OUTPUT
Sum of the number is: 84

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.