Introduction To PL/SQL: Sen Zhang
Introduction To PL/SQL: Sen Zhang
Sen Zhang
Fundamentals
This lecture serves as an
introduction to PL/SQL
a powerful programming language
that works hand in hand with SQL.
Objectives
Learn the fundamentals of the PL/SQL programming
language
How to write and execute PL/SQL programs in
SQL*Plus
Understand PL/SQL data type conversion functions
Manipulate character strings in PL/SQL programs
Learn how to debug PL/SQL programs
10
11
Using SQL, we carefully phrase what we want and then let the
DBMS get it for us.
12
13
An example.
14
Solution
15
17
18
PL/SQL
19
20
Stored?
23
24
Comments
Not executed by interpreter
C style comments (/* ... */) may be used.
25
Reserved word
Data type
Variable
Statement
Arithmetic operation
Logical operation
Control constructs
Loop
Branch
block
Built in functions
How to define user defined functions
How to call user defined functions
26
27
Reserved word
28
29
Data types
30
31
Data type
Description
Sample Declaration
Varchar2
Variable-length
character string
Lastname
varchar2(30)
char
Fixed-length
character string
Gender char(1)
Number
Price number(5,2)
date
Todays_date Date;
These data types are directly from data types used by SQL database
field specification.
32
Some other data types used PL/SQL are more general purpose
programming language oriented, not corresponding to database
data types.
INTEGER
BOOLEAN
DECIMAL
33
34
36
Variables
Variables
Used to store numbers, character strings, dates, and other
data values
Avoid using keywords, table names and column names as
variable names
Must be declared with data type before use: variable_name
data_type_declaration;
Userid
varchar2(10);
38
Remarks
39
Assignment Statements
We can assign values to variables, using the ":=" operator. Like any
other programming languages you might have used before, the
assignment can occur either immediately after the type of the
variable is declared, or anywhere in the executable portion of the
program.
41
Return
Goto <label>
Exit, break a loop
42
Operator
Description
**
Exponentiation
Multiplication
Division
+ and -
negation
43
Meaning
Data Type
Pattern
equal to
all
=X
>
greater than
all
>X
<
less than
all
<X
>=
all
>=X
<=
all
<=X
<> or !=
not equal to
all
<>X
wildcard
Character
single-character wildcard
Character
44
Logical Operators
45
Expressions
46
Built-in functions
47
48
Manipulating Character
Strings with PL/SQL
To concatenate two strings in PL/SQL, you use the
double bar (||) operator:
new_string := string1 || string2;
Manipulating Character
Strings with PL/SQL
To change case, use UPPER, LOWER, INITCAP
INSTR function searches a string for a specific
substring:
start_position := INSTR(original_string, substring);
50
DECLARE
/* Declarative section: variables, types, and local subprograms. */
BEGIN
/* Executable section: procedural and SQL statements go here. */
/* This is the only section of the block that is required. */
EXCEPTION
/* Exception handling section: error handling statements go here. */
END;
51
53
A PL/SQL block
54
If the query returns more than one tuple, you need to use a
cursor,!!!! as described in the future lectures.
55
56
57
Executing a PL/SQL
Program in SQL*Plus
58
59
Logic error:
Program runs but results in an incorrect result
Caused by mistakes at semantic level in
programing
60
62
Objectives
65
66
IF/ELSIF Example
68
Complex Conditions
Created with logical operators AND, OR and NOT
AND is evaluated before OR
Use () to set precedence
69
70
71
72
73
74
Loops
Program structure that executes a series of program
statements, and periodically evaluates an exit
condition to determine if the loop should repeat or
exit
Pretest loop: evaluates the exit condition before any
program commands execute
Posttest loop: executes one or more program
commands before the loop evaluates the exit
condition for the first time
PL/SQL has 5 loop structures
75
76
77
The WHILE...LOOP
WHILE condition LOOP
program statements
END LOOP;
78
79
Cursors
Pointer to a memory location that the DBMS
uses to process a SQL query
Use to retrieve and manipulate database data
80
81
Implicit Cursor
82
83
84
Explicit Cursor
Use for queries that return multiple records or
no records
Must be explicitly declared and used
85
Cursor
In its simplest form, you can think of a cursor as a pointer into a relation in the
database or dynamically generated from other relations.
For example, the following cursor declaration associates the entire employee
table with the cursor named employee_cur:
CURSOR employee_cur
IS
SELECT * FROM employee;
Step 2: Once you have declared the cursor, you can open it:
OPEN employee_cur;
Step 3: And then you can fetch data from it row by row, usually inside a loop
CLOSE employee_cur;
86
87
88
89
90
91
Predefined Exceptions
92
Undefined Exceptions
Less common errors
Do not have predefined names
Must declare your own name for the exception
code in the declaration section
DECLARE
e_exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT(e_exception_name,
-Oracle_error_code);
93
User-Defined Exceptions
Not a real Oracle error
Use to enforce business rules
94
Summary
PL/SQL is a programming language for working with an
Oracle database
Scalar, composite and reference variables can be used
The IF/THEN/ELSE decision control structure allows
branching logic
Five loop constructs allow repeating code
Cursors are returned from queries and can be explicitly
iterated over
Exception handling is performed in the exception section.
User defined exceptions help to enforce business logic
95
Summary
PL/SQL is a programming language for working with
an Oracle database
Scalar, composite and reference variables can be
used
The IF/THEN/ELSE decision control structure allows
branching logic
Five loop constructs allow repeating code
96
97
Stored procedure
Procedure, a function that does not return output through return value.
Prototype, signature, and definition of the procedure,
Procedure body
Stored
Precompiled ?
modular
98