0% found this document useful (0 votes)
15 views

Chapter2 Pl SQL (1)

Uploaded by

brahim damak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter2 Pl SQL (1)

Uploaded by

brahim damak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 3 : PL/SQL Fundamentals

A.U: 2020/2021 1 M(AD)2


Plan

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

variable_name [CONSTANT] datatype [NOT NULL] [:=|DEFAULT expr];

 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

DECLARE v_job takes the type of the column


v_job emp.job%TYPE; job of the table emp.
v_prime NUMBER(5,2) := v_prime is initialized to 500,45.
500.50; v_prime_min takes the type of the
v_prime_min v_prime%TYPE := variable v_prime
v_prime*2;

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.

 To use it, we have to declare a variable with the type nomRecord.

15
3.3 Variables

A type RECORD having


four attributes. Usine is
initialized.

Declaring two variables


with the type RECORD

Initialize record attributes

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.

Primary Key Column


… …
1 Mohamed Toumi
2 Sami Tlili
3 Meriam Bouzid
… …

17
3.3 Variables
 Composed variables – TABLE
 The type TABLE is defined using the following syntax

 The variable declaration is made according to the 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

You might also like