PL-SQL
Oracle PL/SQL WHILE LOOP with Example
What is While Loop? WHILE loop statement works similar to the Basic loop statement except the EXIT...
In PL/SQL, the code is not executed in single line format, but it is always executed by grouping the code into a single element called Blocks. In this tutorial, you are going to learn about these blocks.
Blocks contain both PL/SQL as well as SQL instruction. All these instruction will be executed as a whole rather than executing a single instruction at a time.
In this tutorial, you will learn:
PL/SQL blocks have a pre-defined structure in which the code is to be grouped. Below are different sections of PL/SQL blocks.
The below picture illustrates the different PL/SQL block and their section order.
This is the first section of the PL/SQL blocks. This section is an optional part. This is the section in which the declaration of variables, cursors, exceptions, subprograms, pragma instructions and collections that are needed in the block will be declared. Below are few more characteristics of this part.
Execution part is the main and mandatory part which actually executes the code that is written inside it. Since the PL/SQL expects the executable statements from this block this cannot be an empty block, i.e., it should have at least one valid executable code line in it. Below are few more characteristics of this part.
The exception is unavoidable in the program which occurs at run-time and to handle this Oracle has provided an Exception-handling section in blocks. This section can also contain PL/SQL statements. This is an optional section of the PL/SQL blocks.
The Keyword 'END' marks the end of PL/SQL block.
Below is the syntax of the PL/SQL block structure.
DECLARE --optional
<declarations>
BEGIN --mandatory
<executable statements. At least one executable statement is mandatory>
EXCEPTION --optional
<exception handles>
END; --mandatory
/
Note: A block should always be followed by '/' which sends the information to the compiler about the end of the block.
PL/SQL blocks are of mainly two types.
Anonymous blocks are PL/SQL blocks which do not have any names assigned to them. They need to be created and used in the same session because they will not be stored in the server as database objects.
Since they need not store in the database, they need no compilation steps. They are written and executed directly, and compilation and execution happen in a single process.
Below are few more characteristics of Anonymous blocks.
Named blocks have a specific and unique name for them. They are stored as the database objects in the server. Since they are available as database objects, they can be referred to or used as long as it is present on the server. The compilation process for named blocks happens separately while creating them as a database objects.
Below are few more characteristics of Named blocks.
After this tutorial, you should be aware of PL/SQL blocks and its types, different sections of blocks and their usages. The detailed description of the named PL/SQL blocks will be covered in the later tutorial.
What is While Loop? WHILE loop statement works similar to the Basic loop statement except the EXIT...
What is Dynamic SQL? Dynamic SQL is a programming methodology for generating and running...
What is Object Type in PL/SQL? Object-Oriented Programming is especially suited for building...
Download PDF 1) What is PL SQL ? PL SQL is a procedural language which has interactive SQL, as well as...
What is Collection? A Collection is an ordered group of elements of particular data types. It can...
SQL is the standard language to query a database. PL SQL basically stands for "Procedural Language...