PLSQL

Basic Syntax

Basic Syntax

PL/SQL which is known to be a block-structured language, means these programs are divided and written into the logical blocks of the code. Each of these blocks consists of three subparts, they are:

1. Declarations

This part starts with the keyword Declare. This is an optional section where we will be define all the variables, cursors, subprograms, and all the other elements that are to be used in the program.  

2. Executable commands

This part is enclosed within the keywords BEGIN and END and it is also said to be a mandatory section. This section consists of all the executable PL/SQL statements of the program. This section should also have at least one executable line of the code, which can also be just a Null command for indicating that nothing should be executed.

3.Exception Handling

This starts with the keyword Exception. This is also an optional section contains that can handle errors in the program. 

All the PL/SQL statement ends with the semicolon (;). These blocks can be nested within other PL/SQL blocks using BEGIN and END. Given below is the syntax or the basic structure of a PL/SQL block-

 Syntax:

DECLARE
             <declarations section>
             BEGIN
             <declarations section>
             EXCEPTION
             <declarations section>
             END;
Example:
            DECLARE
               message varchar2(20):= ‘Good Morning!’;
             BEGIN
               dbms_output.put_line(message);
             END;
             /

The end line signifies the end of the block. For running the code from the SQL command line, we also need to give / at the beginning of the very first blank line after the last line of every code.

When we execute the above code in the SQL prompt, it gives us the following output:

Good Morning

PL/SQL procedure successfully completed.