Introduction To PL/SQL
Introduction To PL/SQL
Objectives
At the end of this session, you will be able to: State the need for a procedural language in Oracle Create PL/SQL blocks Write nested blocks Write control structures
Introduction to PL/SQL
Procedural Language/SQL (PL/SQL), is an extension of SQL. The need for PL/SQL arose: To centralize automated business tasks Due to its ability to handle errors
Benefits of PL/SQL
The reasons for using PL/SQL are as follows:
Integrating with the Oracle server and Oracle development
tools
application
Modularizing the development of programs Implementing Portability Declaration of variables Programming with procedural language control structures Handling errors
Integration
PL/SQL integrates with:
The Oracle server because it supports the use of SQL
data types
The Oracle development tools because the tools have
Development tools
Portability
The portability features of PL/SQL are as follows:
PL/SQL programs can be run anywhere the Oracle
Variable Declaration
In PL/SQL, you can declare variables:
To use them in SQL and procedural statements Belonging to different data types Dynamically based on the structure of tables and
Error Handling
PL/SQL implements error handling functionality by:
Processing Oracle server errors with error handling
routines
Allowing users to declare their own error conditions
database
Subprograms, which are named stored code stored in
the database.
PL/SQL Constructs
Constructs include:
Anonymous blocks Procedures Functions Packages Triggers
keywords in uppercase
Use a slash (/) to terminate a PL/SQL block on a line by
itself
Variables
A variable is a named memory location used to store data temporarily.
Variables are used for:
Temporary storage data
Manipulation of data
Reusability of data
Maintenance of data
PL/SQL:
Declare and initialize the variables in the declarative section
of a PL/SQL block
You can specify the initial value of the variable or put a NOT
NULL constraint on it
You can assign new values to variables in the executable
section
Use parameters to assign values to the variables Use output variables to display the results of a PL/SQL block
Types of variables
Variables are of two types:
PL/SQL Variables
The data types used to declare PL/SQL variables are: Scalar data types Composite data types Reference data types LOB data types The syntax for declaring PL/SQL variables is:
identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT value];
NULL constraint
The keyword CONSTANT must precede the data type
Operators in PL/SQL
The following operators can be used in PL/SQL:
Arithmetic
Logical Relational or comparison Concatenation Exponentiation Other miscellaneous symbols
PL/SQL blocks:
Single-row number and character functions Data type conversion functions
Date functions
GREATEST and LEAST functions
Variables of scalar data type are declared with the following syntax:
<variable_name> <datatype [(size)]>
%TYPE Attribute
Use the %TYPE to declare a variable to be of the same data type as that of: A previously declared variable
The syntax is:
<variable_name> variable_name%TYPE
iSQL*Plus Variables
The iSQL*Plus or non-PL/SQL variables that can be used within PL/SQL blocks are:
Substitution variables:Referenced in a block by prefixing it
colon before the variable. The syntax to declare a bind variable is: VARIABLE <variable_name> data type The syntax to display the variable value is: PRINT <variable_name>
Nested Blocks
A block within a block is a nested block
Control Structures
Control structures are categorized into: Conditional constructs
IF - THEN - END IF IF - THEN - ELSE - END IF IF - THEN - ELSIF - END IF
CASE constructs
CASE expressions CASE statements
Loop constructs
Basic Loop FOR Loop WHILE Loop
Summary
In this session you have learnt to:
State the need for a procedural language in Oracle Create PL/SQL blocks
Objectives
At the end of this session, you will be able to:
Raise and handle exceptions Describe the different types of exceptions
Exceptions are handled by: The exception handling section Passing it on to calling environment
Handling Exceptions
To implement exception handling:
Declare an exception Raise the exception Trap and handling the exception Propagate the exception
Trapping exceptions
Exceptions are raised by keyword RAISE EXCEPTION The syntax to handle them is:
EXCEPTION WHEN exception1 [or exceptionN. . .] THEN statements; [WHEN exception 2 [or exceptionN . . .] THEN statements; [WHEN OTHERS THEN statements]
Propagating exceptions
The following figure Illustrates how exceptions propagate.
Types of Exception
The three types of exceptions are:
Predefined exceptions User-defined exceptions
Predefined Exceptions
These exceptions are:
System defined exceptions Raised implicitly
User-Defined Exceptions
These exceptions are handled explicitly The syntax for declaring an exception name is:
<exception> EXCEPTION, where exception is the name
of the exception.
Summary
In this session you have learnt to:
Raise and handle exceptions Describe the different types of exceptions
Objectives
At the end of this session, you will be able to:
Use SELECT statements within PL/SQL blocks Perform data manipulations within PL/SQL blocks Describe the common SQL parser
MERGE statements
The following transaction control options can be used within PL/SQL blocks:
COMMIT
ROLLBACK SAVEPOINT
Cursors
A cursor is an area of memory where the Oracle
server parses and executes SQL statements. Cursors are of the following two kinds:
Implicit Explicit
Implicit Cursor
Implicit cursor attributes are:
SQL%ROWCOUNT SQL%FOUND
SQL%NOTFOUND
SQL%ISOPEN
Oracle9i
Summary
In this session you have learnt to:
Use SELECT statements within PL/SQL blocks Perform data manipulations within PL/SQL blocks Describe the common SQL parser