Sei sulla pagina 1di 6

PL/SQL

What is a PL/SQL ?
PL/SQL is Oracles procedural language that you can use to do such things as
codify your business rules through the creation of stored procedure and packages,
trigger database events to happen or add programming logic to the execution of
SQL command.
PL/SQL overview
PL/SQL code is grouped into structures called blocks. If you create a stored
procedure or package, you give the block of PL/SQL code a name. If the block of
PL/SQL code is not given a name, then it is called an anonymous block.
The PL/SQL block divided into three section: declaration section, the
executable section and the exception section
The structure of a typical PL/SQL block is shown in the listing:
Declare
< declaration section >
begin
< executable commands>
exception
<exception handling>
end;
section
declaration section

description
Defines and initializes the variables and cursor used
in the block
executable commands Uses flow-control commands (such as IF command
and loops)to execute the commands and assign values
to the declared variables
exception handling
Provides handling of error conditions
Example:
Find the circle area and store the results in table named AREAS, where: 1) the
EREAS table has two columns, to store radius and area values.
2) the area of the circle is calculated by squiring the value of the circles
radius and multiplying that value times the constant pi:

The declaration section started with the declare keyword.


The executable section started with the begin keyword.
The end; signals the of PL/SQL block. You may need to add / to execute the
PL/SQL block. When the PL/SQL block is executed, you will receive the following
response from Oracle:
PL/SQL procedure successfully completed

Now select from the database the rows that were inserted by the PL/SQL code.
Exercise:
Find the triangular area and store the results in table named TRIANGULAR.
Conditional Logic
Within PL/SQL, you can use if, else, elseif and case command to control the
flow commands within the Executable Commands section. The formats of the if
clauses are shown in the following listing:

the area of circle example used in the previous section will now be modified to
include conditional logic. In the following listing, the Executable commands
section of the PL/SQL block has been modified to include if and then commands:

Loops
You can use loops to process multiple records within a single PL/SQL block.
PL/SQL supports three types of loops:
Simple loop
FOR loop
WHILE loop

A loop that keeps repeating until an exit or exit when statement is reached
within the loop
A loop repeats a specified number of times
A loop that repeats while a condition is met

Simple loop
In the following example, the loop started by the loop keyword, and the exit
when clause determines when the loop should be exited. An end loop clause
identified the end of the loop.

(Execute and note the results)


The loop should generate multiple entries in the AREAS table. The first record
will be the recode generated by a Radius value of 3. Once an area value exceeds 100,
no more records will be inserted into the AREAS table.
For Loops

In simple loops, the loop executes until an exit condition is met. In a FOR loop
executes a specified number of times. An example of a FOR loop is shown in the
following listing. The FOR loops start by the keyword for, followed by the criteria
used to determine when the processing is complete and the loop can be exited. An
exit command isnt needed within the loop.
In the following example, the areas of circles are calculated 7 times (from 1
through 7).

(Execute and note the results)


While loops
The WHILE loop is similar in structure to the simple loop.
In a while loop, the loop is processed until an exit condition is met. Instead of
specifying the exit condition via an exit command within the loop, the exit condition
is specified in the while command that initiates loop.
In the following listing, a WHILE loop is created so that multiple Radius value
will be processed. If the current value of the radius variable meets the while condition
in the loops specification, then the loops commands are processed. Once a Radius
value fails the while condition in the loops specification the loops execution is
terminated:

(Execute and note the results)

THE EXCEPTION HANDLING SECTION:


The exception handling section begins with the keyword exception, and it
precedes the end command that terminates the Executable Commands section of the
PL/SQL block. The placement of the Exception Handling section with the PL/SQL
block is shown in the following listing:
Declare
< declaration section >
begin
< executable commands>
exception
<exception handling>
end;
The Exception Handling section of a PL/SQL block is optional.
In the following listing, the simple loop for calculating the area of a circle is
shown, with two modifications (shown in bold). A new variable named
some_variable is declared in the declaration section, and a calculation to determine
the variables value is created in the Executable command section.

Because the calculate for some_variable involves division, you may encounter a
situation in which the calculate attempts to divide by zero. The result will be like the
following:

ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 9

Since an error was encountered, the first row inserted into AREA is rolled back,
and the PL/SQL block terminated.
You can modify the processing of the error condition by adding an Exception
Handling section to the PL/SQL block, as shown in the following listing:

(Execute and note the results)


When the PL/SQL block encounters an error, it scans the Exception Handling
section for defined exceptions.
In this case it find the ZERO_DIVIDE exception, which is one of the systemdefined exception available in PL/SQL.
You can use the when other clause to address all exceptions not defined within
your Exception Handling section.
Note
Once an exception is encountered, you cant return to your normal flow of
command processing within the Exception Commands section.

Potrebbero piacerti anche