PLSQL01 Overview
PLSQL01 Overview
1
Benefits of PL/SQL
Integration
Application
2
Benefits of PL/SQL
Improved Performance
SQL
SQL
Application Other DBMSs
SQL
SQL
SQL
IF...THEN
SQL Oracle with
Application ELSE PL/SQL
SQL
END IF;
SQL
3
Benefits of PL/SQL
Modularize program development
DECLARE
BEGIN
EXCEPTION
END;
4
Benefits of PL/SQL
5
PL/SQL Block Structure
• DECLARE – Optional
– Variables, cursors, user-defined
exceptions
• BEGIN – Mandatory
– SQL statements
– PL/SQL statements
• EXCEPTION – Optional
DECLARE
– Actions to perform when
errors occur BEGIN
• END; – Mandatory
EXCEPTION
END;
6
Block Types
Anonymous Procedure Function
[DECLARE] PROCEDURE name FUNCTION name
IS RETURN datatype
IS
BEGIN BEGIN BEGIN
--statements --statements --statements
RETURN value;
[EXCEPTION] [EXCEPTION] [EXCEPTION]
7
Program Constructs
Stored
Anonymous
procedure/
block
DECLARE function
BEGIN Application
Application
procedure/
trigger
function
EXCEPTION
Database
END; Packaged
procedure/
trigger
function
8
Use of Variables
9
Handling Variables in PL/SQL
10
Types of Variables
• PL/SQL variables:
– Scalar
– Composite
– Reference
– LOB (large objects)
• Non-PL/SQL variables: Bind and host
variables
11
Declaring PL/SQL Variables
Syntax
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Examples
Declare
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_comm CONSTANT NUMBER := 1400;
12
Declaring PL/SQL Variables
Guidelines
• Follow naming conventions.
• Initialize variables designated as NOT
NULL.
• Initialize identifiers by using the
assignment operator (:=) or the
DEFAULT reserved word.
13
Naming Rules
• Two variables can have the same name,
provided they are in different blocks.
• The variable name (identifier) should
not be the same as the name of table
columns used in the block.
DECLARE
empno NUMBER(4);
BEGIN
SELECT empno
INTO empno
FROM emp
WHERE ename = 'SMITH';
END;
14
Assigning Values to Variables
Syntax
identifier := expr;
Examples
Set a predefined hiredate for new
employees.
v_hiredate := '31-DEC-98';
15
Variable Initialization and
Keywords
Using:
• Assignment operator (:=)
• DEFAULT keyword
• NOT NULL constraint
16
Base Scalar Datatypes
• VARCHAR2 (maximum_length)
• NUMBER [(precision, scale)]
• DATE
• CHAR [(maximum_length)]
• LONG
• LONG RAW
• BOOLEAN
• BINARY_INTEGER
• PLS_INTEGER
17
Scalar Variable Declarations
Examples
v_job VARCHAR2(9);
v_count BINARY_INTEGER := 0;
v_total_sal NUMBER(9,2) := 0;
v_orderdate DATE := SYSDATE + 7;
c_tax_rate CONSTANT NUMBER(3,2) := 8.25;
v_valid BOOLEAN NOT NULL := TRUE;
18
The %TYPE Attribute
19
Declaring Variables
with the %TYPE Attribute
Examples
...
v_ename emp.ename%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
...
20
Declaring Boolean Variables
• Only the values TRUE, FALSE, and
NULL can be assigned to a Boolean
variable.
• The variables are connected by the
logical operators AND, OR, and NOT.
• The variables always yield TRUE,
FALSE, or NULL.
• Arithmetic, character, and date
expressions can be used to return a
Boolean value.
21
Composite Datatypes
• PL/SQL TABLES
• PL/SQL RECORDS
22
LOB Datatype Variables
Recipe
(CLOB)
Photo
(BLOB)
Movie
(BFILE)
NCLOB
23
Bind Variables
O/S
Bind Variable
Server
24
Referencing Non-PL/SQL
Variables
Store the annual salary into a SQL*Plus
host variable.
Reference non-PL/SQL variables as host
variables.
• Prefix the references with a colon (:).
Variable v number;
BEGIN
:v := v_sal / 12;
END;
/
Print v;
25
DBMS_OUTPUT.PUT_LINE
26