Sie sind auf Seite 1von 7

Objectives

After
After completing
completing this
this lesson,
lesson, you
you should
should be
be
16 able
able toto do
do the
the following:
following:
•• Recognize
Recognize the
the basic
basic PL/SQL
PL/SQL block
block and
and its
its
sections
sections
Declaring Variables
•• Describe
Describe the
the significance
significance of
of variables
variables in
in
PL/SQL
PL/SQL
•• Distinguish
Distinguish between
between PL/SQL
PL/SQL and
and non-
non-
PL/SQL
PL/SQL variables
variables
•• Declare
Declare PL/SQL
PL/SQL variables
variables
•• Execute
Execute aa PL/SQL
PL/SQL block
block
Copyright  Oracle Corporation, 1998. All rights reserved. 16-2 Copyright  Oracle Corporation, 1998. All rights reserved.

PL/SQL Block Structure PL/SQL Block Structure

•• DECLARE
DECLARE––Optional
Optional DECLARE
DECLARE
––Variables,
Variables,cursors,
cursors,user-defined
user-definedexceptions
exceptions v_variable
v_variable VARCHAR2(5);
VARCHAR2(5);
•• BEGIN
BEGIN––Mandatory
Mandatory
BEGIN
BEGIN
SELECT column_name
––SQL
SQLstatements
statements
SELECT column_name
INTO
INTO v_variable
v_variable
––PL/SQL
PL/SQLstatements
statements FROM table_name;
FROM table_name;
•• EXCEPTION
EXCEPTION––Optional
Optional EXCEPTION
EXCEPTION
––Actions
Actionsto
toperform
performwhen
when DECLARE
WHEN
WHEN exception_name
exception_name THEN
THEN DECLARE
errors
errorsoccur
occur ...
...
END; BEGIN
•• END;
END;––Mandatory
Mandatory
BEGIN END;
EXCEPTION EXCEPTION

END;
END;

16-3 Copyright  Oracle Corporation, 1998. All rights reserved. 16-4 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-1


Block Types Program Constructs
Anonymous Procedure Function Stored
Stored
Anonymous
Anonymous
[DECLARE]
[DECLARE] PROCEDURE
PROCEDURE name
name FUNCTION
FUNCTION name
name procedure/
procedure/
block
block
IS
IS RETURN
RETURN datatype
datatype DECLARE function
function
IS
IS
BEGIN
BEGIN BEGIN
BEGIN BEGIN
BEGIN
BEGIN Application
Application
--statements
--statements
--statements --statements
--statements
--statements --statements
--statements
--statements Application
Application procedure/
procedure/
RETURN
RETURN value;
value; trigger
trigger function
function
[EXCEPTION]
[EXCEPTION] [EXCEPTION]
[EXCEPTION] [EXCEPTION]
[EXCEPTION] EXCEPTION
END;
END; END;
END; END;
END;
Database
END; Packaged
Packaged
Database procedure/
trigger procedure/
trigger function
function

16-5 Copyright  Oracle Corporation, 1998. All rights reserved. 16-6 Copyright  Oracle Corporation, 1998. All rights reserved.

Use of Variables Handling Variables in PL/SQL

Use
Use variables
variables for:
for: •• Declare
Declare and
and initialize
initialize variables
variables within
within the
the
•• Temporary
Temporary storage
storage of
of data
data declaration
declaration section.
section.
•• Manipulation
Manipulation of
of stored
stored values
values •• Assign
Assign new
new values
values toto variables
variables within
within the
the
executable section.
executable section.
•• Reusability
Reusability
•• Pass
Pass values
values into
into PL/SQL
PL/SQL blocks
blocks through
through
•• Ease
Ease of
of maintenance
maintenance parameters.
parameters.
•• View
View results
results through
through output
output variables.
variables.

16-7 Copyright  Oracle Corporation, 1998. All rights reserved. 16-8 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-2


Types of Variables Types of Variables
25-OCT-99
•• PL/SQL
PL/SQL variables
variables TRUE “Four score and seven years ago
–– Scalar
Scalar our fathers brought forth upon
–– Composite
Composite this continent, a new nation,
conceived in LIBERTY, and dedicated
–– Reference
Reference
256120.08
to the proposition that all men
–– LOB
LOB (large
(large objects)
objects) are created equal.”

•• Non-PL/SQL
Non-PL/SQL variables
variables Atlanta
–– Bind
Bind and
and host
host variables
variables

16-9 Copyright  Oracle Corporation, 1998. All rights reserved. 16-10 Copyright  Oracle Corporation, 1998. All rights reserved.

Declaring PL/SQL Variables Declaring PL/SQL Variables

Syntax
Syntax Guidelines
Guidelines
identifier [CONSTANT]
identifier datatype [NOT
[CONSTANT] datatype [NOT NULL] •• Follow
[:=
[:= || DEFAULT
DEFAULT expr];
expr];
NULL]
Follow naming
naming conventions.
conventions.
•• Initialize
Initialize variables
variables designated
designated as as NOT
NOT
Examples NULL.
NULL.
Examples
Declare
Declare •• Initialize
Initialize identifiers
identifiers by
by using
using the
the
v_hiredate DATE;
v_hiredate
v_deptno
DATE;
NUMBER(2)
assignment
assignment operator
operator (:=)
(:=) or
or by
by using
using the
the
v_deptno NUMBER(2) NOT
NOT NULL
NULL :=
:= 10;
10;
v_location
v_location VARCHAR2(13)
VARCHAR2(13) :=
:= 'Atlanta';
'Atlanta'; DEFAULT
DEFAULT reserved
reserved word.
word.
c_
c_ comm
comm CONSTANT
CONSTANT NUMBER
NUMBER :=
:= 1400;
1400;
•• Declare
Declare at at most
most one
one identifier
identifier per
per line.
line.

16-11 Copyright  Oracle Corporation, 1998. All rights reserved. 16-12 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-3


Naming Rules Assigning Values to Variables
•• Two
Two variables
variables can
can have
have the
the same
same name,
name, Syntax
Syntax
provided
provided they
they are
are in
in different
different blocks.
blocks. identifier :=
identifier := expr;
expr;
•• The
The variable
variable name
name (identifier)
(identifier) should
should not
not be
be
the Examples
Examples
the same
same asas the
the name
name ofof table
table columns
columns
used
used in
in the
the block.
block. Set
Set aa predefined
predefined hiredate
hiredate for
for new
new employees.
employees.
DECLARE
DECLARE
empno
empno NUMBER(4);
NUMBER(4); v_hiredate
v_hiredate :=
:= '31-DEC-98';
'31-DEC-98';
BEGIN
BEGIN
SELECT
SELECT empno
empno
INTO
INTO empno
empno Set
Set the
the employee
employee name
name to
to ““Maduro.”
Maduro.
Maduro.”
FROM
FROM emp
emp
WHERE
WHERE ename
ename == 'SMITH';
'SMITH'; v_ename
v_ename :=
:= 'Maduro';
'Maduro';
END;
END;

16-13 Copyright  Oracle Corporation, 1998. All rights reserved. 16-14 Copyright  Oracle Corporation, 1998. All rights reserved.

Variable Initialization and Scalar Datatypes


Keywords
•• Hold
Hold aa single
single value
value
Using •• Have
Have no
no internal
internal components
components
•• :=
:= Assignment
Assignment Operator
Operator
•• DEFAULT
DEFAULT 25-OCT-99
“Four score and seven years
•• NOT
NOT NULL
NULL ago our fathers brought
forth upon this continent, a
TRUE
new nation, conceived in

256120.08 LIBERTY, and dedicated to


the proposition that all men
are created equal.”
Atlanta
16-15 Copyright  Oracle Corporation, 1998. All rights reserved. 16-16 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-4


Base Scalar Datatypes Scalar Variable Declarations
•• VARCHAR2
VARCHAR2 (maximum_length)
(maximum_length)
•• NUMBER
NUMBER [(precision,
[(precision, scale)]
scale)] Examples
Examples
v_job VARCHAR2(9);
•• DATE
DATE v_job
v_count
VARCHAR2(9);
BINARY_INTEGER
v_count BINARY_INTEGER :=
:= 0;
0;
v_total_sal
v_total_sal NUMBER(9,2)
NUMBER(9,2) :=
:= 0;
0;
•• CHAR
CHAR [(maximum_length)]
[(maximum_length)] v_orderdate
v_orderdate DATE
DATE :=
:= SYSDATE
SYSDATE ++ 7;
7;
c_tax_rate
c_tax_rate CONSTANT
CONSTANT NUMBER(3,2)
NUMBER(3,2) :=:= 8.25;
8.25;
•• LONG
LONG v_valid
v_valid BOOLEAN
BOOLEAN NOT
NOT NULL
NULL :=
:= TRUE;
TRUE;
•• LONG
LONG RAW
RAW
•• BOOLEAN
BOOLEAN
•• BINARY_INTEGER
BINARY_INTEGER
•• PLS_INTEGER
PLS_INTEGER
16-17 Copyright  Oracle Corporation, 1998. All rights reserved. 16-18 Copyright  Oracle Corporation, 1998. All rights reserved.

The %TYPE Attribute Declaring Variables with the


%TYPE Attribute
•• Declare
Declare aa variable
variable according
according to:
to:
–– A
A database
database column
column definition
definition
Examples
Examples
–– Another
Another previously
previously declared
declared variable
variable ...
...
v_ename emp.ename%TYPE;
•• Prefix
Prefix %TYPE
%TYPE with:
with: v_ename
v_balance
emp.ename%TYPE;
NUMBER(7,2);
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE
v_balance%TYPE :=
:= 10;
–– The
The database
database table
table and
and column
column v_min_balance
...
10;
...
–– The
The previously
previously declared
declared variable
variable name
name

16-19 Copyright  Oracle Corporation, 1998. All rights reserved. 16-20 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-5


Declaring BOOLEAN Variables Composite Datatypes
•• Only
Only the
the values
values TRUE,
TRUE, FALSE,
FALSE, and
and NULL
NULL
can
can be
be assigned
assigned toto aa Boolean
Boolean variable.
variable. Types
Types
•• The
The variables
variables are
are connected
connected by by the
the logical
logical •• PL/SQL
PL/SQL TABLES
TABLES
operators
operators AND,
AND, OR,
OR, andand NOT.
NOT. •• PL/SQL
PL/SQL RECORDS
RECORDS
•• The
The variables
variables always
always yield
yield TRUE,
TRUE, FALSE,
FALSE, or
or
NULL.
NULL.
•• Arithmetic,
Arithmetic, character,
character, and
and date
date expressions
expressions
may
may be used to return a Boolean value.
be used to return a Boolean value.

16-21 Copyright  Oracle Corporation, 1998. All rights reserved. 16-22 Copyright  Oracle Corporation, 1998. All rights reserved.

LOB Datatype Variables Bind Variables


Recipe
(CLOB)

Photo
(BLOB)
O/S
Movie Bind Variable
(BFILE)
Server

NCLOB

16-23 Copyright  Oracle Corporation, 1998. All rights reserved. 16-24 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-6


Referencing Non-PL/SQL Variables Summary

•• PL/SQL
PL/SQL blocks
blocks are
are composed
composed of
of the
the
Store
Store the
the annual
annual salary
salary into
into aa SQL*Plus
SQL*Plus host
host following sections:
following sections:
DECLARE
variable.
variable. –– Declarative
Declarative (optional)
(optional) BEGIN
:g_monthly_sal
:g_monthly_sal :=
:= v_sal
v_sal // 12;
12; –– Executable
Executable (required)
(required) EXCEPTION
•• Reference
Reference non-PL/SQL
non-PL/SQL variables
variables as
as host
host –– Exception
Exception handling
handling (optional)
(optional) END;
variables.
variables. •• A
A PL/SQL
PL/SQL block
block can
can be
be an
an
•• Prefix
Prefix the
the references
references with
with aa colon
colon (:).
(:). anonymous
anonymous block, procedure, or
block, procedure, or
function.
function.

16-25 Copyright  Oracle Corporation, 1998. All rights reserved. 16-26 Copyright  Oracle Corporation, 1998. All rights reserved.

Summary Practice Overview

•• PL/SQL
PL/SQL identifiers:
identifiers: •• Determining
Determining validity
validity of
of declarations
declarations
–– Are
Are defined
defined in
in the
the declarative
declarative section
section •• Developing
Developing aa simple
simple PL/SQL
PL/SQL block
block
–– Can
Can be
be of
of scalar,
scalar, composite,
composite, reference,
reference,
or
or LOB
LOB datatype
datatype
–– Can
Can be
be based
based on on the
the structure
structure of
of another
another
variable
variable or
or database
database object
object
–– Can
Can be
be initialized
initialized

16-27 Copyright  Oracle Corporation, 1998. All rights reserved. 16-28 Copyright  Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-7

Das könnte Ihnen auch gefallen