Chapter2 Pl SQL (1)
Chapter2 Pl SQL (1)
1. Introduction
2. Structures of a PL/SQL block
3. Variables
4. Nested blocks
2
3.1 Introduction
SQL is a non-procedural language.
The development of an application around a RDB requires the use of:
Variables
programming control structures (loops and alternatives).
Need a procedural language to link several SQL queries with variables and in
the usual control structures = 4GL (4th Generation language).
Hence PL/SQL: procedural and structured programming language for
developing applications around relational databases.
3
3.1 Introduction
PL/SQL: Procedural Language for SQL.
PL/SQL is a language that integrates SQL and allows programming in a
procedural way. It is specific to Oracle. For SQL Server there is for example
an equivalent: TRANSAC SQL.
4
3.2 Structures of PL/SQL block
PL/SQL programs are structured in blocks corresponding to logical units of
the program (procedures, functions).
A block is made up of three parts: the declarative part, the execution part, and
the error handling part.
5
3.2 Structures of PL/SQL block
Example:
6
3.2 Structures of PL/SQL block
Header:
The header allows you to specify the type of the block (procedure or function).
Declaration part:
Contains the description of the structures and variables used in the block.
Optional section.
Starts with the keyword DECLARE.
7
3.2 Structures of PL/SQL block
Execution part:
Contains the program instructions and possibly, at the end, the error handling section.
Mandatory.
Introduced by the keyword BEGIN.
Ends with the keyword END.
Error handling part:
Optional.
Introduced by the keyword EXCEPTION.
8
3.3 Variables
Les The variables can be of any SQL type, or of the BOOLEAN type (with two
possible values: TRUE and FALSE).
SQL types: CHAR, NUMBER, DATE,VARCHAR2.
PL/SQL types: BOOLEAN, SMALLINT, BINARY_INTEGER, DECIMAL, FLOAT,
INTEGER, REAL, ROWID.
The variable declaration includes the name of the variable, its type, and
possibly an initialization value.
9
3.3 Variables
There are two types of variables that work under PL/SQL :
PL/SQL variables
Scalar variable: containing a single value
Composite variable: containing several values such as RECORD, TABLE, NESTED TABLE,VARRAY
Reference variable: variable that points to a data type
LOB: variable locator of large objects such as images and videos
Non-PL / SQL variables
Substitution variables
Host variables
10
3.3 Variables
Declaration et initialization
datatype : le the data type of the variable, which is either scalar, compound, reference,
or LOB.
CONSTANT : constrain the variable to be a constant
NOT NULL : this keyword forces the variable to contain a value.
expr : initial value of a variable, can be a literal value, another variable, or an
expression involving operators and functions.
11
3.3 Variables
The declaration of several variables on the same line is prohibited.
Assignment
variable := expression;
DEFAULT : constrain the variable to be constante
SELECT expression INTO variable FROM...
Cannot assign the value NULL to a variable declared NOT NULL (the error
VALUE_ERROR is returned).
The NOT NULL constraint must be followed by an initialization.
12
3.3 Variables
Scalar variables
The % TYPE keyword declares a variable with the same type of another variable or a
column of an existing table or view.
PL/SQL Comments
13
3.3 Variables
Composed variables – RECORD
The %ROWTYPE declares a RECORD variable having the same structure as the
table/view row.
PL/SQL Comments
DECLARE Emp_record takes the structure of a row
emp_record emp%ROWTYPE; from the table emp.
v_sal NUMBER:=800;
BEGIN Access to the row attributes
emp_record.empno:=2564;
emp_record.sal:=v_sal+100;
…
Note that with the %ROWTYPE, attributes do not inherit the NOT NULL constraint.
14
3.3 Variables
Composed variables – RECORD
To define a personalized record, we have to define the RECORD type.
15
3.3 Variables
RECORD assignment
16
3.3 Variables
Composed variables – TABLE
The type TABLE is used to declare dynamic tables (without a fixed size)
A variable with the type TABLE has two columns, a primairy key having the type BINARY_INTEGER
and a column having the scalar or RECORD type.
17
3.3 Variables
Composed variables – TABLE
The type TABLE is defined using the following syntax
18
3.3 Variables
19
3.3 Variables
Composed variables – TABLE
There are procedures and functions that allow you to manipulate TABLE variables:
20
3.4 Nested Blocks
21