Está en la página 1de 32

1

1
1-1
Antrix Consultancy Services
Vasumathi M
Declaring Variables
Declaring Variables
1-2
Antrix Consultancy Services
Vasumathi M
Objectives
Objectives
After completing this lesson, you should
be able to do the following:
Recognize the basic PL/SQL block and
its sections
Describe the significance of variables in
PL/SQL
Distinguish between PL/SQL and non-
PL/SQL variables
Declare PL/SQL variables
Execute a PL/SQL block
After completing this lesson, you should
After completing this lesson, you should
be able to do the following:
be able to do the following:
Recognize the basic PL/SQL block and
its sections
Describe the significance of variables in
PL/SQL
Distinguish between PL/SQL and non-
PL/SQL variables
Declare PL/SQL variables
Execute a PL/SQL block
1-3
Antrix Consultancy Services
Vasumathi M
PL/SQL Block Structure
PL/SQL Block Structure
DECLARE Optional
Variables, cursors, user-defined
exceptions
BEGIN Mandatory
SQL statements
PL/SQL statements
EXCEPTION Optional
Actions to perform when
errors occur
END; Mandatory
DECLARE Optional
Variables, cursors, user-defined
exceptions
BEGIN Mandatory
SQL statements
PL/SQL statements
EXCEPTION Optional
Actions to perform when
errors occur
END; Mandatory
DECLARE DECLARE
BEGIN BEGIN
EXCEPTION EXCEPTION
END; END;
1-4
Antrix Consultancy Services
Vasumathi M
PL/SQL Block Structure
PL/SQL Block Structure
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
DECLARE DECLARE
BEGIN BEGIN
EXCEPTION EXCEPTION
END; END;
1-5
Antrix Consultancy Services
Vasumathi M
Block Types
Block Types
Anonymous
Anonymous
Procedure
Procedure
Function
Function
[DECLARE]
BEGIN
--statements
[EXCEPTION]
END;
[DECLARE] [DECLARE]
BEGIN BEGIN
-- --statements statements
[EXCEPTION] [EXCEPTION]
END; END;
PROCEDURE name
IS
BEGIN
--statements
[EXCEPTION]
END;
PROCEDURE name PROCEDURE name
IS IS
BEGIN BEGIN
-- --statements statements
[EXCEPTION] [EXCEPTION]
END; END;
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
FUNCTION name FUNCTION name
RETURN RETURN datatype datatype
IS IS
BEGIN BEGIN
-- --statements statements
RETURN value; RETURN value;
[EXCEPTION] [EXCEPTION]
END; END;
1-6
Antrix Consultancy Services
Vasumathi M
Program Constructs
Program Constructs
Anonymous
block
Anonymous Anonymous
block block
Application
trigger
Application Application
trigger trigger
Stored
procedure/
function
Stored Stored
procedure/ procedure/
function function
Database
trigger
Database Database
trigger trigger
Application
procedure/
function
Application Application
procedure/ procedure/
function function
Packaged
procedure/
function
Packaged Packaged
procedure/ procedure/
function function
DECLARE DECLARE
BEGIN BEGIN
EXCEPTION EXCEPTION
END; END;
1-7
Antrix Consultancy Services
Vasumathi M
Use of Variables
Use of Variables
Use variables for:
Temporary storage of data
Manipulation of stored values
Reusability
Ease of maintenance
Use variables for:
Use variables for:
Temporary storage of data
Manipulation of stored values
Reusability
Ease of maintenance
1-8
Antrix Consultancy Services
Vasumathi M
Handling Variables in PL/SQL
Handling Variables in PL/SQL
Declare and initialize variables within
the declaration section.
Assign new values to variables within
the executable section.
Pass values into PL/SQL blocks through
parameters.
View results through output variables.
Declare and initialize variables within
the declaration section.
Assign new values to variables within
the executable section.
Pass values into PL/SQL blocks through
parameters.
View results through output variables.
1-9
Antrix Consultancy Services
Vasumathi M
Types of Variables
Types of Variables
PL/SQL variables
Scalar
Composite
Reference
LOB (large objects)
Non-PL/SQL variables
Bind and host variables
PL/SQL variables
Scalar
Composite
Reference
LOB (large objects)
Non-PL/SQL variables
Bind and host variables
1-10
Antrix Consultancy Services
Vasumathi M
TRUE
TRUE
Types of Variables
Types of Variables
25-OCT-99
25-OCT-99
Atlanta
Atlanta
Four score and seven years ago
our fathers brought forth upon
this continent, a new nation,
conceived in LIBERTY, and dedicated
to the proposition that all men
are created equal.
256120.08
256120.08
1-11
Antrix Consultancy Services
Vasumathi M
Declaring PL/SQL Variables
Declaring PL/SQL Variables
Syntax
Examples
Syntax
Syntax
Examples
Examples
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Declare
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_ comm CONSTANT NUMBER := 1400;
Declare
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_ comm CONSTANT NUMBER := 1400;
1-12
Antrix Consultancy Services
Vasumathi M
Declaring PL/SQL Variables
Declaring PL/SQL Variables
Guidelines
Follow naming conventions.
Initialize variables designated as NOT
NULL.
Initialize identifiers by using the
assignment operator (:=) or by using the
DEFAULT reserved word.
Declare at most one identifier per line.
Guidelines
Guidelines
Follow naming conventions.
Initialize variables designated as NOT
NULL.
Initialize identifiers by using the
assignment operator (:=) or by using the
DEFAULT reserved word.
Declare at most one identifier per line.
1-13
Antrix Consultancy Services
Vasumathi M
Naming Rules
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.
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;
DECLARE
empno NUMBER(4);
BEGIN
SELECT empno
INTO empno
FROM emp
WHERE ename = 'SMITH';
END;
1-14
Antrix Consultancy Services
Vasumathi M
Assigning Values to Variables
Assigning Values to Variables
v_hiredate := '31-DEC-98';
v_hiredate := '31-DEC-98';
Syntax
Examples
Set a predefined hiredate for new
employees.
Syntax
Syntax
Examples
Examples
Set a predefined
Set a predefined
hiredate
hiredate
for new
for new
employees.
employees.
identifier := expr;
identifier := expr;
Set the employee name to Maduro.
Set the employee name to
Set the employee name to
Maduro
Maduro
.
.
v_ename := 'Maduro';
v_ename := 'Maduro';
1-15
Antrix Consultancy Services
Vasumathi M
Variable Initialization and
Keywords
Variable Initialization and
Keywords
Using
:= Assignment Operator
DEFAULT
NOT NULL
Using
:= Assignment Operator
DEFAULT
NOT NULL
1-16
Antrix Consultancy Services
Vasumathi M
Scalar Datatypes
Scalar Datatypes
Hold a single value
Have no internal components
Hold a single value
Have no internal components
25-OCT-99
25-OCT-99
Atlanta
Atlanta
Four score and seven years
ago our fathers brought
forth upon this continent, a
new nation, conceived in
LIBERTY, and dedicated to
the proposition that all men
are created equal.
TRUE
TRUE
256120.08
256120.08
1-17
Antrix Consultancy Services
Vasumathi M
Base Scalar Datatypes
Base Scalar Datatypes
VARCHAR2 (maximum_length)
NUMBER [(precision, scale)]
DATE
CHAR [(maximum_length)]
LONG
LONG RAW
BOOLEAN
BINARY_INTEGER
PLS_INTEGER
VARCHAR2 (maximum_length)
NUMBER [(precision, scale)]
DATE
CHAR [(maximum_length)]
LONG
LONG RAW
BOOLEAN
BINARY_INTEGER
PLS_INTEGER
1-18
Antrix Consultancy Services
Vasumathi M
Scalar Variable Declarations
Scalar Variable Declarations
Examples
Examples
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;
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;
1-19
Antrix Consultancy Services
Vasumathi M
The %TYPE Attribute
The %TYPE Attribute
Declare a variable according to:
A database column definition
Another previously declared variable
Prefix %TYPE with:
The database table and column
The previously declared variable
name
Declare a variable according to:
A database column definition
Another previously declared variable
Prefix %TYPE with:
The database table and column
The previously declared variable
name
1-20
Antrix Consultancy Services
Vasumathi M
Declaring Variables with the
%TYPE Attribute
Declaring Variables with the
%TYPE Attribute
Examples
Examples
Examples
...
v_ename emp.ename%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
...
...
v_ename emp.ename%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
...
1-21
Antrix Consultancy Services
Vasumathi M
Declaring BOOLEAN Variables
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 may be used to return a
Boolean value.
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 may be used to return a
Boolean value.
1-22
Antrix Consultancy Services
Vasumathi M
Composite Datatypes
Composite Datatypes
Types
PL/SQL TABLES
PL/SQL RECORDS
Types
Types
PL/SQL TABLES
PL/SQL RECORDS
1-23
Antrix Consultancy Services
Vasumathi M
LOB Datatype Variables
LOB Datatype Variables
Recipe
Recipe
(CLOB)
(CLOB)
Photo
Photo
(BLOB)
(BLOB)
Movie
Movie
(BFILE)
(BFILE)
NCLOB
NCLOB
1-24
Antrix Consultancy Services
Vasumathi M
Bind Variables
Bind Variables
Server
O/S
O/S
Bind Variable
Bind Variable
1-25
Antrix Consultancy Services
Vasumathi M
Referencing Non-PL/SQL
Variables
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 (:).
Store the annual salary into a SQL*Plus
Store the annual salary into a SQL*Plus
host variable.
host variable.
Reference non-PL/SQL variables as
host variables.
Prefix the references with a colon (:).
:g_monthly_sal := v_sal / 12;
:g_monthly_sal := v_sal / 12;
1-26
Antrix Consultancy Services
Vasumathi M
Summary
Summary
PL/SQL blocks are composed of
the following sections:
Declarative (optional)
Executable (required)
Exception handling (optional)
A PL/SQL block can be an
anonymous block, procedure, or
function.
PL/SQL blocks are composed of
the following sections:
Declarative (optional)
Executable (required)
Exception handling (optional)
A PL/SQL block can be an
anonymous block, procedure, or
function.
DECLARE DECLARE
BEGIN BEGIN
EXCEPTION EXCEPTION
END; END;
1-27
Antrix Consultancy Services
Vasumathi M
Summary
Summary
PL/SQL identifiers:
Are defined in the declarative section
Can be of scalar, composite,
reference, or LOB datatype
Can be based on the structure of
another variable or database object
Can be initialized
PL/SQL identifiers:
Are defined in the declarative section
Can be of scalar, composite,
reference, or LOB datatype
Can be based on the structure of
another variable or database object
Can be initialized
1-28
Antrix Consultancy Services
Vasumathi M
Practice Overview
Practice Overview
Determining validity of declarations
Developing a simple PL/SQL block
Determining validity of declarations
Developing a simple PL/SQL block
1-29
Antrix Consultancy Services
Vasumathi M
1-30
Antrix Consultancy Services
Vasumathi M
1-31
Antrix Consultancy Services
Vasumathi M
1-32
Antrix Consultancy Services
Vasumathi M

También podría gustarte