Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Dullea
CSC8490
Introduction to PL/SQL
Dr. James Dullea
professor@comcast.net
Module 01-9
Revised: June 12, 2005
Introdu
Slide 1 of 36
Introduction to PL/SQL
What is PL/SQL
Why PL/SQL
Kinds of PL/SQL BLOCKS
Slide 2 of
What is PL/SQL
PL/SQL is a sophistical programming language used to
access an Oracle database from a various environments.
PL/SQL stands for Procedural Language/SQL.
It extends SQL by adding constructs found in other
procedural languages, such as: loops, conditional
statements, declared variables, accessing individual
records one at a time, and many others.
Slide 3 of
Slide 4 of
Slide 5 of
*/
BEGIN
/* Executable section (required).
*/
EXCEPTION
/* Exception handling section
(optional). */
END;
/
Dr. James Dullea, CSC8490
Introduction to
Slide 6 of
Slide 7 of
Slide 8 of
SET SERVEROUTPUT ON
DECLARE
v_name
VARCHAR2(30);
v_title
VARCHAR2(20);
BEGIN
v_name := 'James Dullea';
v_title := 'Research Scientist';
DBMS_OUTPUT.PUT_LINE(v_name||' works as a '||
v_title);
END;
/
Slide 9 of
VARCHAR2(15);
employee.salary%TYPE;
Slide 10
Data Types
Scalar type
Numeric: INT,
DEC,NUMBER,BINARY_INTEGER
Composite types
Date: DATE
Reference types
Lob types
Slide 11
CURSORS
CLOSE c_query;
Slide 12
CURSORS
Slide 13
Conditional Statements
Conditional Processing
The specified
conditions are
evaluated by the
system and the result
determines which
sequence of
statements is to be
carried out.
Slide 14
Slide 15
Loop Structures
Unconstrained loops
WHILE loop
FOR loop
GOTO <<LABEL>>
Slide 16
Unconstrained Loops
LOOP
<sequence of statements>
EXIT WHEN <condition>
<sequence of statements>
END LOOP;
Slide 17
WHILE LOOP
WHILE <condition>
LOOP
<statements>
END LOOP;
Note: The loop will continue to
process as long as the condition is
TRUE or an EXIT (or EXIT WHEN)
statement is encountered.
Dr. James Dullea, CSC8490
Introduction to
Slide 18
FOR LOOP
FOR <loop_counter> IN [REVERSE]
<low bound>..<high bound>
LOOP
<sequence of statements>
END LOOP;
Slide 19
GOTO statement
GOTO label;
Example:
LOOP
<sequence of statements>
IF <condition> THEN
GOTO get_out_of_loop;
<sequence of statements>
END LOOP;
<<get_out_of_loop>>
Dr. James Dullea, CSC8490
Introduction to
Slide 20
NAMED BLOCKS
The following are types of NAMED BLOCKS
Stored Procedures
Similar to an anonymous block except it can be
stored in the database, can accept
parameters, and can be executed over and over
again (with different parameters)
Functions
Type of named blocks that is executed within a
DML or SQL statement. It may take in one or
more parameters and RETURNs only one value
back to the calling application.
Triggers
A named block that executes only when an
associated DML statement is executed, such as
an INSERT, UPDATE, or DELETE statement.
Slide 21
(Procedures or Functions)
Program Comments
Header
IS|AS
Declaration Section
BEGIN
Executable Section
EXCEPTION
Exception Section
END;
/
(optional)
(mandatory)
(mandatory)
(mandatory)
(optional)
(mandatory)
Slide 22
Slide 23
Execution of a Stored
Functions
EXEC
Slide 24
Slide 25
Slide 26
Slide 27
Execution of a Stored
Functions
SELECT fname||' '||lname AS "EMPLOYEE",
CalcStateTax(salary) AS "STATE TAX"
FROM employee;
Slide 28
Slide 29
What is a Trigger
Similar to stored procedures and functions.
Contains a Declaration, Executable, and
Exception sections
Differences
Triggers are not executed explicitly, they are
implicitly execute when a triggering event
occurs. (This is called firing the trigger)
Triggers do not accept parameters
Triggering events are fired by DML Statements
( INSERTs, UPDATEs, or DELETEs) against
tables or views AND certain system events
Dr. James Dullea, CSC8490
Introduction to
Slide 30
Slide 31
EXCEPTION
(optional)
Exception Section
END;
(mandatory)
NOTE: a_trigger_event may be any combination of an
INSERT, DELETE, and/or UPDATE on a table or view
Dr. James Dullea, CSC8490
Slide 32
Introduction to
Slide 33
Error Handling
When errors occur during the execution,
control will be branched to the exception
handling section.
A corresponding error handler will be
found to deal with the error.
Slide 34
Practice 1
Use the Company database schema, write a stored
procedure to add an employee to the employee
table, using parameters to input the data. Use
your name and following information to test the
procedure.
FNAME
MINIT
LNAME
SSN
BDATE
STREET
CITY
STATE
ZIP
SEX
SALARY
SUPERSSN
DNO
Slide 35
Practice 2
Write a function (called GetDay) that will take in a
date as a parameter and return the actual name
of the day for that date. Use the function to
solve the following problem.
Using the data in the employee table from
Assignment 1, write an SQL statement or an
anonymous block (containing the above
function GetDay) that uses your first and last
name in a where clause to access the record
and returns the actual day of the week that you
were born. Hint: GetDay(bdate)
Slide 36