Professional Documents
Culture Documents
Abdou Illia
Lesson B Objectives
After completing this lesson, you should be able to:
Create PL/SQL decision control structures
Use SQL queries in PL/SQL programs
Create loops in PL/SQL programs
Create PL/SQL tables and tables of records
Use cursors to retrieve database data into PL/SQL
programs
Use the exception section to handle errors in
PL/SQL programs
IF/THEN
Question: What two lines do you need to change to make the program
display Today is not Friday when the current day is different than
Friday?
4
IF/THEN/ELSE
Syntax:
IF condition THEN
commands that execute if condition is TRUE;
ELSE
commands that execute if condition is FALSE;
END IF;
IF/THEN/ELSE
Nested IF/THEN/ELSE
IF/ELSIF
10
Loops
Systematically executes program statements
Periodically evaluates exit condition to determine if
loop should repeat or exit
Pretest loop
Evaluates exit condition before any program
commands execute
Posttest loop
Executes program commands before loop evaluates
exit condition for first time
12
Pretest or posttest
Syntax:
Syntax
LOOP
[program statements]
IF condition THEN
EXIT;
END IF;
[additional program
statements]
END LOOP;
LOOP
program statements
EXIT WHEN condition;
END LOOP;
The WHILE...LOOP
Syntax
WHILELOOP
is a Pretest loop
Cursor
A pointer to memory location on database server
Used to:
Retrieve and manipulate database data in PL/SQL
programs
Types:
Implicit cursor
Explicit cursor
Implicit Cursors
Context area
A memory location created by INSERT, UPDATE,
DELETE, or SELECT
Contains information about query (# rows, etc.)
Active set
Set of data rows that query retrieves when a SELECT query
is issued
Implicit cursor
A pointer to the context area
Called so, because you do not need to write code to
explicitly create the cursor or retrieve its values
Used to assign output of SELECT query to PL/SQL program
variables when query will return only one record*
* Error occurs if query returns no records or more than one record
Explicit Cursors
Retrieve and display data in PL/SQL programs for
query that might
Retrieve multiple records
Return no records at all
Declare cursor
Open cursor
Fetch data rows
Close cursor
The declared
variable is used
to retrieve the
cursor content
and to display
Runtime errors
Exception handling
Programmers place commands in EXCEPTION section
DECLARE
variable declarations
BEGIN
program statements
EXCEPTION
error-handling statements
END;
- undefined exception
Predefined Exceptions
Most common errors that occur in programs
PL/SQL language:
Assigns exception name
Provides built-in exception handler for each predefined exception
Undefined Exceptions
Less common errors
Do not have predefined names
Must explicitly declare exception in programs
declaration section
Associate new exception with specific Oracle error
code
Create exception handler in exception section
Using same syntax as for predefined exceptions
DECLARE
e_exception_name EXCEPTION;
PRAGMA EXCEPTION_UNIT(e_exception_name, -Oracle_error_code);
User-defined Exceptions
Used to handle an exception that
Does not raise Oracle runtime error
But requires exception handling to
Enforce business rules or
Ensure integrity of database
Example:
Internal Northwoods rule is Users can delete row
from the ENROLLMENT table only if s_grade is
NULL
Trying to delete a delete an ENROLLMENT row
where the s_grade is not NULL will raise an
exception that needs to be handled