Академический Документы
Профессиональный Документы
Культура Документы
PL/SQL
2-4
Using Scalar Data Types
Objectives
This lesson covers the following objectives:
Declare and use scalar data types in PL/SQL
Define guidelines for declaring and initializing PL/SQL
variables
Identify the benefits of anchoring data types with the %TYPE
attribute
PLSQL 2-4
Using Scalar Data Types
Purpose
Most of the variables you define and use in PL/SQL have
scalar data types.
A variable can have an explicit data type, such as VARCHAR2,
or it can automatically have the same data type as a table
column in the database. You will learn the benefits of basing
some variables on table columns.
PLSQL 2-4
Using Scalar Data Types
VARCHAR2(9);
VARCHAR2(6);
VARCHAR2(10);
LONG;
PLSQL 2-4
Using Scalar Data Types
PLSQL 2-4
Using Scalar Data Types
DATE := SYSDATE + 7;
DATE;
TIMESTAMP;
PLSQL 2-4
Using Scalar Data Types
PLSQL 2-4
Using Scalar Data Types
PLSQL 2-4
Using Scalar Data Types
PLSQL 2-4
Using Scalar Data Types
10
PLSQL 2-4
Using Scalar Data Types
11
PLSQL 2-4
Using Scalar Data Types
12
%TYPE Attribute
Look at this database table and the PL/SQL block that uses it.
This PL/SQL block stores the correct salary in the
v_emp_salary variable. But what if the table column is altered
later?
CREATE TABLE myemps (
emp_name
VARCHAR2(6),
emp_salary
NUMBER(6,2));
DECLARE
v_emp_salary
NUMBER(6,2);
BEGIN
SELECT emp_salary INTO v_emp_salary
FROM myemps WHERE emp_name = 'Smith';
END;
PLSQL 2-4
Using Scalar Data Types
13
PLSQL 2-4
Using Scalar Data Types
14
table.column_name%TYPE;
Examples:
...
v_emp_lname
v_balance
v_min_balance
...
employees.last_name%TYPE;
NUMBER(7,2);
v_balance%TYPE := 1000;
PLSQL 2-4
Using Scalar Data Types
15
PLSQL 2-4
Using Scalar Data Types
16
%TYPE Attribute
Look again at the database table and the PL/SQL block. Now
the PL/SQL block continues to work correctly even if the
column data type is altered later.
CREATE TABLE myemps (
emp_name
VARCHAR2(6),
emp_salary
NUMBER(6,2));
DECLARE
v_emp_salary
myemps.emp_salary%TYPE;
BEGIN
SELECT emp_salary INTO v_emp_salary
FROM myemps WHERE emp_name = 'Smith';
END;
PLSQL 2-4
Using Scalar Data Types
17
Terminology
Key terms used in this lesson included:
%TYPE
Boolean
PLSQL 2-4
Using Scalar Data Types
18
Summary
In this lesson, you should have learned how to:
Declare and use scalar data types in PL/SQL
Define guidelines for declaring and initializing PL/SQL
variables
Identify the benefits of anchoring data types with the %TYPE
attribute
PLSQL 2-4
Using Scalar Data Types
19