System Logic: Oracle PL/SQL
System Logic: Oracle PL/SQL
ORACLE PL/SQL
Overview of PL/SQL
Course Objectives
After completing this course, you should be able
to
do the following:
• Describe the purpose of PL/SQL
• Describe the use of PL/SQL for the developer
as well as the DBA
• Explain the benefits of PL/SQL
• Create, execute, and maintain procedures,
functions, packages, and database triggers
• Manage PL/SQL subprograms and triggers
• Describe Oracle supplied packages
• Manipulate large objects (LOBs)
About PL/SQL
Oracle server
Benefits of PL/SQL
Integration
Application
Improved performance
SQL
SQL
Application Other DBMSs
SQL
SQL
SQL
IF...THEN
SQL Oracle with
Application ELSE PL/SQL
SQL
END IF;
SQL
Benefits of PL/SQL
…
BEGIN
…
EXCEPTION
END;
Benefits of PL/SQL
• PL/SQL is portable.
• You can declare variables.
Benefits of PL/SQL
• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity
Invoking Stored Procedures
and Functions
Scott LOG_EXECUTION
procedure
1 xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
2 vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
3 vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx vvvvvvvvvvvvvv
Oracle Oracle Oracle vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
Portal Discoverer Forms vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
Developer vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
4
Scott
Summary
• PL/SQL is an extension to SQL.
• Blocks of PL/SQL code are passed to and
processed by a PL/SQL engine.
• Benefits of PL/SQL:
– Integration
– Improved performance
– Portability
– Modularity of program development
• Subprograms are named PL/SQL blocks,
declared as either procedures or functions.
• You can invoke subprograms from different
environments.
Declaring Variables
Objectives
…
BEGIN
…
EXCEPTION
END;
Executing
DECLARE
Statements and
PL/SQL Blocks
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
…
DECLARE
…
BEGIN
…
EXCEPTION
END;
Block Types
Anonymous
[DECLARE]
Procedure
PROCEDURE name
Function
FUNCTION name
IS RETURN datatype
IS
BEGIN BEGIN BEGIN
--statements --statements --statements
RETURN value;
[EXCEPTION] [EXCEPTION] [EXCEPTION]
…
BEGIN
…
EXCEPTION
END;
Tools Constructs Database Server
Anonymous blocks Constructs
Application procedures or Anonymous blocks
functions Stored procedures or
Application packages functions
Application triggers Stored packages
Object types Database triggers
Object types
Use of Variables
• PL/SQL variables:
– Scalar
– Composite
– Reference
– LOB (large objects)
• Non-PL/SQL variables: Bind and host
variables
Using iSQL*Plus Variables
Within PL/SQL Blocks
• PL/SQL does not have input or output
capability of its own.
Atlanta
Declaring PL/SQL Variables
Syntax:
Syntax:
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Examples:
Examples:
DECLARE
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_comm CONSTANT NUMBER := 1400;
Guidelines for Declaring PL/SQL
Variables
• Follow naming conventions.
• Initialize variables designated as NOT
NULL and CONSTANT.
• Declare one identifier per line.
• Initialize identifiers by using the
assignment
identifier operator (:=) or the
:= expr;
Syntax:
v_hiredate := '01-JAN-2001';
v_ename := 'Maduro';
Examples:
Scalar Data Types
• Hold a single value
• Have no internal components
TRUE
ago our fathers brought
forth upon this continent, a
new nation, conceived in
256120.08 LIBERTY, and dedicated to
the proposition that all men
Atlanta
are created equal.”
Base Scalar Data Types
• CHAR [(maximum_length)]
• VARCHAR2 (maximum_length)
• LONG
• LONG RAW
• NUMBER [(precision,
scale)]
• BINARY_INTEGER
• PLS_INTEGER
• BOOLEAN
Base Scalar Data Types
• DATE
• TIMESTAMP
• TIMESTAMP WITH TIME ZONE
• TIMESTAMP WITH LOCAL TIME
ZONE
• INTERVAL YEAR TO MONTH
• INTERVAL DAY TO SECOND
Scalar Variable Declarations
Examples:
DECLARE
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;
...