CLO1 - 01-PLSQLIntroduction
CLO1 - 01-PLSQLIntroduction
VARIABLES
EXCEPTIONS
docs.oracle.com
Types of Blocks
Anonymous
Function Procedure Trigger
block
• Named block • Named block • A trigger is a • Not stored
that is stored • Can process PL/SQL block since it cannot
on Oracle several associated be referenced
server variables with a specific by a name
• Accepts zero • Returns no table, view, • Usually
or more input values schema, or a embedded in
parameters • Interacts with database an application
• Returns one application • Executes program,
value program using implicitly stored in a
IN, OUT, or whenever a script file, or
INOUT particular manually
parameter event occurs entered when
INTRODUCTION TO ORACLE PL/SQL needed 8
PL/SQL Block Structure
Has 3 sections:
Declaration section (Optional)
Variables, cursors, user-defined exceptions
Execution section (Required)
SQL statements
PL/SQL statements
Exception section (Optional)
Actions to perform when errors occur
Comment statements
Enclosed within /* and */
docs.oracle.com
Reserves a temporary storage area
in the computer’s memory
Every variable must have:
A name
A datatype
Declaring
Variables can be initialized
Variables
Variables that have a value that
does not change during the
execution of the block can be
designated as constant using the
optional keyword CONSTANT
Sequential processing
Processes statements one after another
Loop repeats statements multiple times until WHILE Loop repeats statements while
it reaches exit condition condition is TRUE
SELECT statement
with an INTO
clause
docs.oracle.com
Declaring and opening a cursor
docs.oracle.com
Fetching data from a Cursor
ow e
t r cti v
n a
rre h e
e cu set oft
th ti ve ws t
nly ac
l l ro se
d o the d a
a in a
Lo Lo
docs.oracle.com
Cursors and records
Process the rows of the active set by fetching values into a PL/SQL
record
docs.oracle.com
Use a FOR LOOP to process an explicit cursor
FOR LOOP statement allows you to fetch every row from a cursor
without manually managing the execution cycle i.e., OPEN, FETCH,
and CLOSE.
FOR record IN cursor_name
LOOP
process_record_statements;
END LOOP;
docs.oracle.com
An exception is a PL/SQL error that
is raised during program exceution
An exception can be raised :
Implicity by the Oracle server
Handling Explicitly by the program
Exceptions An exception can be handled
By trapping it with a handler
By propagating it to the calling
environment
docs.oracle.com
Trapping Exceptions
Often involve:
Misspelling reserved word
Omitting required character in command
Using built-in function improperly
Interpreter
Flags line number and character location of syntax errors
May actually be on preceding line
Missing
SEMI_COLON (;)
Caused by:
Not using proper order of operations in arithmetic functions
Passing incorrect parameter values to built-in functions
Creating loops that do not terminate properly
Using data values that are out of range or not of right data type