Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
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.
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).
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: