PL SQL
PL SQL
Chapter 4
A Guide to Oracle9i 1
Lesson A Objectives
A Guide to Oracle9i 2
Fundamentals of PL/SQL
• Full-featured programming language
• An interpreted language
• Type in editor, execute in SQL*Plus
A Guide to Oracle9i 3
Variables and Data Types
• Variables
– Used to store numbers, character strings, dates,
and other data values
– Avoid using keywords, table names and column
names as variable names
– Must be declared with data type before use:
variable_name data_type_declaration;
A Guide to Oracle9i 4
Scalar Data Types
A Guide to Oracle9i 5
Scalar Data Types
A Guide to Oracle9i 6
Composite and Reference Variables
• Composite variables
– RECORD: contains multiple scalar values, similar to a table
record
– TABLE: tabular structure with multiple columns and rows
– VARRAY: variable-sized array
• Reference variables
– Directly reference a specific database field or record and
assume the data type of the associated field or record
– %TYPE: same data type as a database field
– %ROWTYPE: same data type as a database record
A Guide to Oracle9i 7
PL/SQL Program Blocks
• Comments:
– Not executed by interpreter
– Enclosed between /* and */
– On one line beginning with --
A Guide to Oracle9i 8
Arithmetic Operators
A Guide to Oracle9i 9
Assignment Statements
A Guide to Oracle9i 10
Executing a PL/SQL
Program in SQL*Plus
A Guide to Oracle9i 12
Manipulating Character
Strings with PL/SQL
• To concatenate two strings in PL/SQL, you use the
double bar (||) operator:
– new_string := string1 || string2;
• To remove blank leading spaces use the LTRIM
function:
– string := LTRIM(string_variable_name);
• To remove blank trailing spaces use the RTRIM
function:
– string := RTRIM(string_variable_name);
• To find the number of characters in a character string
use the LENGTH function:
– string_length := LENGTH(string_variable_name);
A Guide to Oracle9i 13
Manipulating Character
Strings with PL/SQL
• To change case, use UPPER, LOWER, INITCAP
• INSTR function searches a string for a specific
substring:
– start_position := INSTR(original_string, substring);
• SUBSTR function extracts a specific number of
characters from a character string, starting at a given
point:
– extracted_string := SUBSTR(string_variable, starting_point,
number_of_characters);
A Guide to Oracle9i 14
Debugging PL/SQL Programs
• Syntax error:
– Command does not follow the guidelines of the
programming language
– Generates compiler or interpreter error messages
• Logic error:
– Program runs but results in an incorrect result
– Caused by mistake in program
A Guide to Oracle9i 15
Finding and Fixing Syntax Errors
A Guide to Oracle9i 16
Finding and Fixing Logic Errors
A Guide to Oracle9i 17
Lesson B Objectives
A Guide to Oracle9i 21
Complex Conditions
• Created with logical operators AND, OR and NOT
• AND is evaluated before OR
• Use () to set precedence
A Guide to Oracle9i 22
Using SQL Queries in PL/SQL Programs
A Guide to Oracle9i 23
Loops
• Program structure that executes a series of program
statements, and periodically evaluates an exit
condition to determine if the loop should repeat or
exit
• Pretest loop: evaluates the exit condition before any
program commands execute
• Posttest loop: executes one or more program
commands before the loop evaluates the exit
condition for the first time
• PL/SQL has 5 loop structures
A Guide to Oracle9i 24
The LOOP...EXIT Loop
LOOP
[program statements]
IF condition THEN
EXIT;
END IF;
[additional program statements]
END LOOP
A Guide to Oracle9i 25
The LOOP...EXIT WHEN Loop
LOOP
program statements
EXIT WHEN condition;
END LOOP;
A Guide to Oracle9i 26
The WHILE...LOOP
A Guide to Oracle9i 27
The Numeric FOR Loop
A Guide to Oracle9i 28
Cursors
A Guide to Oracle9i 29
Implicit Cursor
A Guide to Oracle9i 30
Using an Implicit Cursor
A Guide to Oracle9i 31
Explicit Cursor
A Guide to Oracle9i 32
Using an Explicit Cursor
• Declare the cursor
– CURSOR cursor_name IS select_query;
• Open the cursor
– OPEN cursor_name;
• Fetch the data rows
– LOOP
FETCH cursor_name INTO variable_name(s);
EXIT WHEN cursor_name%NOTFOUND;
• Close the cursor
– CLOSE cursor_name;
A Guide to Oracle9i 33
Explicit Cursor with %ROWTYPE
A Guide to Oracle9i 34
Cursor FOR Loop
A Guide to Oracle9i 35
Using Cursor FOR Loop
A Guide to Oracle9i 36
Handling Runtime Errors
in PL/SQL Programs
• Runtime errors cause exceptions
• Exception handlers exist to deal with different error situations
• Exceptions cause program control to fall to exception section
where exception is handled
A Guide to Oracle9i 37
Predefined Exceptions
A Guide to Oracle9i 38
Undefined Exceptions
A Guide to Oracle9i 40
Summary
• PL/SQL is a programming language for working with an
Oracle database
• Scalar, composite and reference variables can be used
• The IF/THEN/ELSE decision control structure allows
branching logic
• Five loop constructs allow repeating code
• Cursors are returned from queries and can be explicitly
iterated over
• Exception handling is performed in the exception section.
User defined exceptions help to enforce business logic
A Guide to Oracle9i 41