3
Declaring PL/SQL Variables
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Objectives
After completing this lesson, you should be able to do the
following:
• Recognize valid and invalid identifiers
• List the uses of variables
• Declare and initialize variables
• List and describe various data types
• Identify the benefits of using the %TYPE attribute
• Declare, use, and print bind variables
3-2 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Agenda
• Introducing variables
• Examining variable data types and the %TYPE attribute
• Examining bind variables
3-3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Use of Variables
Variables can be used for:
• Temporary storage of data
• Manipulation of stored values
• Reusability
SELECT
first_name,
department_id
Jennifer v_fname
INTO
v_fname,
v_deptno
FROM …
10 v_deptno
3-4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Requirements for Variable Names
A variable name:
• Must start with a letter
• Can include letters or numbers
• Can include special characters (such as $, _, and #)
• Must contain no more than 30 characters
• Must not include reserved words
3-5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Handling Variables in PL/SQL
Variables are:
• Declared and (optionally) initialized in the declarative
section
• Used and assigned new values in the executable section
• Passed as parameters to PL/SQL subprograms
• Used to hold the output of a PL/SQL subprogram
3-6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Declaring and Initializing PL/SQL Variables
Syntax:
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Examples:
DECLARE
v_hiredate DATE;
v_location VARCHAR2(13) := 'Atlanta';
v_deptno NUMBER(2) NOT NULL := 10;
c_comm CONSTANT NUMBER := 1400;
3-7 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Declaring and Initializing PL/SQL Variables
DECLARE
v_myName VARCHAR(20);
1 BEGIN
DBMS_OUTPUT.PUT_LINE('My name is: '||v_myName );
v_myName := 'John';
DBMS_OUTPUT.PUT_LINE('My name is: '||v_myName );
END;
/
DECLARE
v_myName VARCHAR2(20):= 'John';
2 BEGIN
v_myName := 'Steven';
DBMS_OUTPUT.PUT_LINE('My name is: '|| v_myName);
END;
/
3-8 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Delimiters in String Literals
DECLARE
v_event VARCHAR2(15);
BEGIN
v_event := q'!Father's day!';
DBMS_OUTPUT.PUT_LINE('3rd Sunday in June is :
'|| v_event );
v_event := q'[Mother's day]';
DBMS_OUTPUT.PUT_LINE('2nd Sunday in May is :
'|| v_event );
END;
/
Resulting
output
3-9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Agenda
• Introducing variables
• Examining variable data types and the %TYPE attribute
• Examining bind variables
3 - 10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Types of Variables
• PL/SQL variables:
– Scalar
– Reference
– Large object (LOB)
– Composite
• Non-PL/SQL variables: Bind variables
3 - 11 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Types of Variables
15-JAN-09
TRUE
Snow White
Long, long ago,
in a land far, far away,
there lived a princess called Snow
White. . .
Atlanta
256120.08
3 - 12 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Guidelines for Declaring and Initializing
PL/SQL Variables
• Follow consistent naming conventions.
• Use meaningful identifiers for variables.
• Initialize variables that are designated as NOT NULL and
CONSTANT.
• Initialize variables with the assignment operator (:=) or the
DEFAULT keyword:
v_myName VARCHAR2(20):='John';
v_myName VARCHAR2(20) DEFAULT 'John';
• Declare one identifier per line for better readability and
code maintenance.
3 - 13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Guidelines for Declaring PL/SQL Variables
• Avoid using column names as identifiers.
DECLARE
employee_id NUMBER(6);
BEGIN
SELECT employee_id
INTO employee_id
FROM employees
WHERE last_name = 'Kochhar';
END;
/
• Use the NOT NULL constraint when the variable must hold
a value.
3 - 14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Naming Conventions of PL/SQL
Structures Used in This Course
PL/SQL Structure Convention Example
Variable v_variable_name v_rate
Constant c_constant_name c_rate
Subprogram p_parameter_name p_id
parameter
Bind (host) variable b_bind_name b_salary
Cursor cur_cursor_name cur_emp
Record rec_record_name rec_emp
Type type_name_type ename_table_type
Exception e_exception_name e_products_invalid
File handle f_file_handle_name f_file
3 - 15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Scalar Data Types
• Hold a single value
• Have no internal components
TRUE 15-JAN-09
The soul of the lazy man
desires, and he has nothing;
but the soul of the diligent
shall be made rich.
256120.08 Atlanta
3 - 16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Base Scalar Data Types
• CHAR [(maximum_length)]
• VARCHAR2 (maximum_length)
• NUMBER [(precision, scale)]
• BINARY_INTEGER
• PLS_INTEGER
• BOOLEAN
• BINARY_FLOAT
• BINARY_DOUBLE
3 - 17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Base Scalar Data Types
• DATE
• TIMESTAMP
• TIMESTAMP WITH TIME ZONE
• TIMESTAMP WITH LOCAL TIME ZONE
• INTERVAL YEAR TO MONTH
• INTERVAL DAY TO SECOND
3 - 19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Declaring Scalar Variables
Examples:
DECLARE
v_emp_job VARCHAR2(9);
v_count_loop BINARY_INTEGER := 0;
v_dept_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;
...
3 - 21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
%TYPE Attribute
• Is used to declare a variable according to:
– A database column definition
– Another declared variable
• Is prefixed with:
– The database table and column name
– The name of the declared variable
3 - 22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Declaring Variables
with the %TYPE Attribute
Syntax
identifier table.column_name%TYPE;
Examples
...
v_emp_lname employees.last_name%TYPE;
...
...
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 1000;
...
3 - 24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Declaring Boolean Variables
• Only the TRUE, FALSE, and NULL values can be assigned
to a Boolean variable.
• Conditional expressions use the logical operators AND and
OR, and the unary operator NOT to check the variable
values.
• The variables always yield TRUE, FALSE, or NULL.
• Arithmetic, character, and date expressions can be used to
return a Boolean value.
3 - 25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
LOB Data Type Variables
Book
(CLOB)
Photo
(BLOB)
Movie
(BFILE)
NCLOB
3 - 26 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Composite Data Types: Records and Collections
PL/SQL Record:
TRUE 23-DEC-98 ATLANTA
PL/SQL Collections:
1 SMITH 1 5000
2 JONES 2 2345
3 NANCY 3 12
4 TIM 4 3456
VARCHAR2 NUMBER
PLS_INTEGER PLS_INTEGER
3 - 27 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Agenda
• Introducing variables
• Examining variable data types and the %TYPE attribute
• Examining bind variables
3 - 28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Bind Variables
Bind variables are:
• Created in the environment
• Also called host variables
• Created with the VARIABLE keyword*
• Used in SQL statements and PL/SQL blocks
• Accessed even after the PL/SQL block is executed
• Referenced with a preceding colon
Values can be output using the PRINT command.
* Required when using SQL*Plus and SQL Developer
3 - 29 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Referencing Bind Variables
Example:
VARIABLE b_emp_salary NUMBER
BEGIN
SELECT salary INTO :b_emp_salary
FROM employees WHERE employee_id = 178;
END;
/
PRINT b_emp_salary
SELECT first_name, last_name
FROM employees
WHERE salary=:b_emp_salary;
Output
3 - 31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Using AUTOPRINT with Bind Variables
3 - 32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Quiz
The %TYPE attribute:
a. Is used to declare a variable according to a database
column definition
b. Is used to declare a variable according to a collection of
columns in a database table or view
c. Is used to declare a variable according to the definition of
another declared variable
d. Is prefixed with the database table and column name or
the name of the declared variable
3 - 33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Recognize valid and invalid identifiers
• Declare variables in the declarative section of a PL/SQL
block
• Initialize variables and use them in the executable section
• Differentiate between scalar and composite data types
• Use the %TYPE attribute
• Use bind variables
3 - 34 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Practice 3: Overview
This practice covers the following topics:
• Determining valid identifiers
• Determining valid variable declarations
• Declaring variables within an anonymous block
• Using the %TYPE attribute to declare variables
• Declaring and printing a bind variable
• Executing a PL/SQL block
3 - 35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.