PL SQL Intro1
PL SQL Intro1
What is PL/SQL
PL/SQL is a sophistical programming language used to
access an Oracle database from a various environments.
• Easy to read
– With modularity features and error handling
Using PL/SQL as a programming
language
• Permits all operations of standard programming
languages e.g.
– Conditions IF-THEN-ELSE-END IF;
– Jumps GOTO
• WHILE loops
• WHILE condition LOOP
...SQL Statements...
END LOOP;
• FOR loops
• FOR <variable(numeric)> IN [REVERSE]
<lowerbound>..<upperbound> LOOP .... ..... END LOOP;
PL/SQL Control Structure
• Cursor
DECLARE
name varchar2(20);
Cursor c1 is
select t.name
from table t
where date is not null;
BEGIN
OPEN c1;
LOOP
FETCH c1 into name;
exit when c1%NOTFOUND;
END LOOP;
CLOSE c1;
END;
A Sample UNIVERSITY database
• A sample database containing the following relations
to illustrate PL/SQL programming:
cursor emp_cursor is
select * from employee;
emp emp_cursor%ROWTYPE;
ename employee.name%type;
Variable Declaration
• PL/SQL variables can be of the same type as
database columns
– Varchar2(20), date, number(5), …
– Assignments
– Programming constructs such as IF … THEN
… ELSE … END IF, WHILE, FOR, GOTO …
– SQL data manipulation statements
– OPEN and CLOSE cursor statements
if SQL%NOTFOUND then
INSERT INTO STUDENTS (id, fname,
lname, major) VALUES
(stu_seq.nextval, v_fname,
v_lname, v_major);
end if;
Cursor Attributes
• A cursor has 4 attributes each returning a value that can be
used in expressions.
• CURSOR_ALREADY_OPEN
• DUP_VAL_ON_INDEX (during insert)
• INVALID_CURSOR (cursor not open)
• INVALID_NUMBER (e.g. ’34+some’)
• NO_DATA_FOUND (select .. Into returns no rows)
• TOO_MANY_ROWS (select returns more than 1 row)
• VALUE_ERROR (assignment or select into)
• ZERO_DIVIDE
• OTHERS
Exception example
• declare
• x number := 5;
• y number;
• z number;
• begin
• z := sqrt(x);
• y := 1 / z;
• z := x + y;
• Exception
• when others then
• handle_error(…);
• end;
• /
A PL/SQL Block
• declare
• <declaration section>
• begin
• <executable section>
• exception
• <exception handling section>
• end
Kinds of PL/SQL BLOCKS
The basic unit in any PL/SQL PROGRAM is a BLOCK.
All PL/SQL programs are composed of a single block or
blocks that occur either sequentially or nested within
another block. There are two kinds of blocks:
Anonymous blocks are generally constructed
dynamically and executed only once by the user. It is
sort of a complex SQL statement.
Named blocks are blocks that have a name associated
with them, are stored in the database, and can be
executed again and again, can take in parameters, and
can modify and existing database.
Structure of Anonymous Block
DECLARE
/* Declare section (optional). */
BEGIN
/* Executable section (required). */
EXCEPTION
/* Exception handling section
(optional). */
8 rows selected.
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)