Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Declaring Variables
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 nonPL/SQL variables Declare PL/SQL variables Execute a PL/SQL block
16-2 Copyright Oracle Corporation, 1998. All rights reserved.
16-3
16-4
Block Types
Anonymous
[DECLARE] BEGIN --statements [EXCEPTION] END;
Procedure
PROCEDURE name IS BEGIN --statements [EXCEPTION] END;
Function
FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END;
16-5
Program Constructs
Anonymous block Stored procedure/ function Application procedure/ function
Application trigger
Database trigger
END;
16-6
Use of Variables
Use variables for: Temporary storage of data Manipulation of stored values Reusability Ease of maintenance
16-7
16-8
Types of Variables
PL/SQL variables
Scalar Composite Reference LOB (large objects)
Non-PL/SQL variables
Bind and host variables
16-9 Copyright Oracle Corporation, 1998. All rights reserved.
Types of Variables
TRUE
25-OCT-99
Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in LIBERTY, and dedicated
256120.08
16-10
Atlanta
Examples
Declare v_hiredate v_deptno v_location c_ comm DATE; NUMBER(2) NOT NULL := 10; VARCHAR2(13) := 'Atlanta'; CONSTANT NUMBER := 1400;
16-11
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.
DECLARE empno NUMBER(4); BEGIN SELECT empno INTO empno FROM emp WHERE ename = 'SMITH'; END;
16-13 Copyright Oracle Corporation, 1998. All rights reserved.
16-14
16-15
Scalar Datatypes
Hold a single value Have no internal components
Four score 25-OCT-99 and seven years ago our fathers brought new nation, conceived in
TRUE
256120.08
16-16
LIBERTY, and dedicated to the proposition that all men are created equal.
Atlanta
16-18
name
16-19
16-20
Composite Datatypes
Types PL/SQL TABLES PL/SQL RECORDS
16-22
16-23
Bind Variables
16-24
Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:).
16-25
Summary
PL/SQL blocks are composed of the following sections:
Declarative (optional) Executable (required) Exception handling (optional)
Summary
PL/SQL identifiers:
Are defined in the declarative section Can be of scalar, composite,
16-27
Practice Overview
Determining validity of declarations Developing a simple PL/SQL block
16-28