PL SQL Final
PL SQL Final
An Introduction to
PL/SQL
Mehdi Azarmi
2
Introduction
• PL/SQL is Oracle's procedural language extension to
SQL, the non-procedural relational database language.
• Combines power and flexibility of SQL (4GL) with
procedural constructs of a 3GL
• Extends SQL by adding
• Variables and types
• Control Structures (conditional, loops)
• Procedures and functions
• Exception handling
•…
3
Block Definition
• Basic unit of PL/SQL is a block
• Three possible sections of a block
• Declarative section
• Executable section
• Exception handling
• A block performs a logical unit of work in the program
• Blocks can be nested
4
Block Structure
DECLARE
BEGIN
EXCEPTION
END;
5
Executable Section
• The only required section
• PL/SQL is not case sensitive. C style comments (/* ... */) may be
used.
6
Variables - Example
DECLARE
a NUMBER := 3;
BEGIN
a := a + 1;
END;
.
To execute the program
run;
Example
CREATE TABLE T1(
e INTEGER,
f INTEGER • There is only one tuple of T1 that has
);
first component greater than 1, (2,4).
DELETE FROM T1; Therefore, INSERT statement inserts
INSERT INTO T1 VALUES(1, 3); (4,2) into T1.
INSERT INTO T1 VALUES(2, 4);
/* Above is plain SQL; below is the PL/SQL • The SELECT statement in PL/SQL
program. */
only works if the result of the query
DECLARE
contains a single tuple
a NUMBER;
b NUMBER;
• If the query returns more than one
BEGIN
SELECT e,f INTO a,b FROM T1 WHERE e>1; tuple, you need to use a cursor
INSERT INTO T1 VALUES(b,a);
END;
.
run;
9
Control flow in
PL/SQL
10
IF Statement
• An IF statement looks like:
IF <condition>
THEN <statement_list>
ELSE <statement_list>
END IF;
• The ELSE part is optional
• If you want a multiway branch, use:
IF <condition_1> THEN …
ELSIF <condition_2> THEN …
...
ELSIF <condition_n> THEN …
ELSE …
END IF;
11
IF - Example
DECLARE
a NUMBER;
b NUMBER;
BEGIN
SELECT e,f INTO a,b FROM T1 WHERE e>1;
IF b=1 THEN
INSERT INTO T1 VALUES(b,a);
ELSE
INSERT INTO T1 VALUES(b+10,a+10);
END IF;
END;
.
run;
12
IF - Example 2
IF TotalStudents = 0 THEN
DECLARE
INSERT INTO temp_table (char_col)
TotalStudents NUMBER; VALUES ('There are no students
registered');
BEGIN ELSIF TotalStudents < 5 THEN
INSERT INTO temp_table (char_col)
SELECT COUNT(*)
VALUES ('There are only a few students
INTO TotalStudents registered');
ELSIF TotalStudents < 10 THEN
FROM students; INSERT INTO temp_table (char_col)
VALUES ('There are a little more
… students registered');
ELSE
INSERT INTO temp_table (char_col)
VALUES ('There are many students
registered');
END IF;
END;
/
13
Loops
• A loop allows execution of a set of statements repeatedly
• Types of loops
• Simple loop
• Numeric For loop
• While loop
• Loops are created with the following:
LOOP
<loop_body> /* A list of statements. */
END LOOP;
• At least one of the statements in <loop_body> should be
an EXIT statement of the form
• EXIT WHEN <condition>;
15
LOOP - Example
DECLARE
i NUMBER := 1;
BEGIN
LOOP
INSERT INTO T1 VALUES(i,i);
i := i+1;
EXIT WHEN i>100;
END LOOP;
END;
.
run;
16
FOR - Example
BEGIN
FOR LoopCounter IN 1..50 LOOP
INSERT INTO temp_table (num_col)
VALUES (LoopCounter);
END LOOP;
END;
/
18
Cursors
• the SELECT statement in PL/SQL only works if the result
of the query contains a single tuple
• If the query returns more than one tuple, or you want to
manipulate a relation with more than one row, you need to
use a cursor
• A cursor creates a named context area as a result of
executing an associated SQL statement
• Permits the program to step through the multiple rows
displayed by an SQL statement
19
… Next page
20
Procedure
• PROCEDURE and FUNCTIONS
• Parameters
• Mode of operation:
• IN (read-only)
• OUT (write-only)
• INOUT (read and write)
• Type
• the type specifier in a parameter declaration must be
unconstrained.
• Example: CHAR(10) and VARCHAR(20) are illegal
• CHAR or VARCHAR should be used instead.
22
PROCEDURE - Template
CREATE OR REPLACE PROCEDURE PROCNAME(PARAMETERS) AS
<local_var_declarations>
BEGIN
<procedure_body>
END;
.
run;
• The run at the end runs the statement that creates the procedure; it does not
execute the procedure.
• To execute the procedure, use another PL/SQL statement, in which the
procedure is invoked as an executable statement.
• For example:
BEGIN addtuple1(99); END;
.
Run;
23
PROCEDURE – Example 1
CREATE TABLE T2 (
a INTEGER,
b CHAR(10)
);
BEGIN
INSERT INTO T2(a, b)
VALUES(x, y);
END addtuple2;
.
run;
PROCEDURE – Example 2
CREATE TABLE T3 (
a INTEGER,
b INTEGER
);
DECLARE
v NUMBER;
BEGIN
addtuple3(10, v); /* second parameter should be an lvalue*/
END;
.
run;
25
Printing
• Always use the following line (setting output buffer) at the
beginning of your SQL file:
set serveroutput on size 32000
• Printing a line:
dbms_output.put_line(VAR1|| '. ' || VAR2);
• You may declare and use a bind variable to print a local variable
VARIABLE x NUMBER
BEGIN
:x := 1;
END;
.
run;
PRINT :x;
27
Debugging
• PL/SQL does not always tell you about compilation errors.
Instead, it gives you a cryptic message such as:
"procedure created with compilation errors".
• If you don't see what is wrong immediately, try issuing the
command
show errors procedure <procedure_name>;
• Alternatively, you can type, SHO ERR (short for SHOW
ERRORS) to see the most recent compilation error.
• Note that the location of the error given as part of the
error message is not always accurate!
28
Performance of PL/SQL
• SQL results in many network trips, one for each SQL
statement
• PL/SQL permits several SQL statements to be bundled
into a single block
• Results in fewer calls to database
• Less network traffic
• faster response time
29
References
• http://infolab.stanford.edu/~ullman/fcdb/oracle/or-
plsql.html
• Oracle PL/SQL Programming: Covers Versions Through
Oracle Database 11g Release 2, by Steven Feuerstein
and Bill Pribyl (Oct 1, 2009)