Академический Документы
Профессиональный Документы
Культура Документы
Dullea
CSC8490
Introduction to PL/SQL
Dr. James Dullea
professor@comcast.net
Module 01-9
Revised: June 12, 2005
Slide 1 of 36
Introduction to PL/SQL
What is PL/SQL
Why PL/SQL
Kinds of PL/SQL BLOCKS
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.
*/
BEGIN
/* Executable section (required).
*/
EXCEPTION
/* Exception handling section
(optional). */
END;
/
Dr. James Dullea, CSC8490
Introduction to PL/SQL
Slide 6 of 36
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;
/
VARCHAR2(15);
employee.salary%TYPE;
Dr. James Dullea, CSC8490
Introduction to PL/SQL
Slide 10 of 36
Data Types
Scalar type
Numeric: INT,
DEC,NUMBER,BINARY_INTEGER
Composite types
Reference types
Lob types
CURSORS
CLOSE c_query;
CURSORS
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.
Loop Structures
Unconstrained loops
WHILE loop
FOR loop
GOTO <<LABEL>>
Unconstrained Loops
LOOP
<sequence of statements>
EXIT WHEN <condition>
<sequence of statements>
END LOOP;
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 PL/SQL
Slide 18 of 36
FOR LOOP
FOR <loop_counter> IN [REVERSE]
LOOP
<sequence of statements>
END LOOP;
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 PL/SQL
Slide 20 of 36
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.
Dr. James Dullea, CSC8490
Introduction to PL/SQL
Slide 21 of 36
(Procedures or Functions)
Program Comments
Header
IS|AS
Declaration Section
BEGIN
Executable Section
EXCEPTION
Exception Section
END;
/
(optional)
(mandatory)
(mandatory)
(mandatory)
(optional)
(mandatory)
Execution of a Stored
Functions
EXEC AddDepartment
JUN-2005');
Execution of a Stored
Functions
SELECT fname||' '||lname AS "EMPLOYEE",
CalcStateTax(salary) AS "STATE TAX"
FROM employee;
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 PL/SQL
Slide 30 of 36
EXCEPTION (optional)
Exception Section
END;
(mandatory)
Slide 32 of 36
Error Handling
When errors occur during the execution,
control will be branched to the exception
handling section.
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
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)