Beruflich Dokumente
Kultur Dokumente
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.
•• 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.
16-5 Copyright Oracle Corporation, 1998. All rights reserved. 16-6 Copyright Oracle Corporation, 1998. All rights reserved.
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.
•• 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.
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.
16-13 Copyright Oracle Corporation, 1998. All rights reserved. 16-14 Copyright Oracle Corporation, 1998. All rights reserved.
16-19 Copyright Oracle Corporation, 1998. All rights reserved. 16-20 Copyright Oracle Corporation, 1998. All rights reserved.
16-21 Copyright Oracle Corporation, 1998. All rights reserved. 16-22 Copyright Oracle Corporation, 1998. All rights reserved.
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.
•• 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.
•• 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.