PLSQL Part1 Declaring Variables
PLSQL Part1 Declaring Variables
PL/SQL
Part 1 - Declaring Variables
Prepared by:
Sherwin O. Bautista
Course Objectives
2
IBM SOLUTIONS DELIVERY INC.
DECLARE - Optional
Variables, cursors,
user-defined exceptions
BEGIN - Mandatory
DECLARE
-- SQL statements
{code .... } -- PL/SQL statements
END;
3
IBM SOLUTIONS DELIVERY INC.
Declarative Contains all variables, constants, cursors, and user-define exceptions that are Optional
referenced in the executable and declarative sections
Executable Contains SQL statements to manipulate data in the database and PL/SQL statements Mandatory
to manipulate data in the block
Exception Specifies the actions to perform when errors and abnormal conditions arise in the Optional
Handling executable section
4
IBM SOLUTIONS DELIVERY INC.
Place a semicolon (;) at the end of a SQL statement or PL/SQL control statement
When the block is executed successfully, without unhandled errors or compile errors,
the message output should be as follows:
PL/SQL procedure successfully completed.
END and all other PL/SQL statements require a semicolon to terminate the
statement.
5
IBM SOLUTIONS DELIVERY INC.
Block Types
6
IBM SOLUTIONS DELIVERY INC.
Block Types
Anonymous Blocks:
unnamed blocks
named PL/SQL blocks that can accept parameters and can be invoked.
Program Construct
The program construct are available based on the environment in which they are executed.
8
IBM SOLUTIONS DELIVERY INC.
Use of Variables
Variable can be use for the following:
While validating data input and for processing later in the data flow process, data
can be temporarily stored.
Manipulation of stored values:
Variables can be used for calculations and other data manipulations without
accessing the database.
Reusability:
Ease of maintenance:
When using %TYPE and %ROWTYPE, you declare variables, basing the
declarations on the definitions of database columns. If definition changes, the
variable declaration changes accordingly at run time.
9
IBM SOLUTIONS DELIVERY INC.
IN OUT -- use this parameter to pass initial values to the subprogram being
called and to return updated values to the caller.
10
IBM SOLUTIONS DELIVERY INC.
Types of Variables
PL/SQL supports four data type categories that can use for declaring variables, constant,
and pointers.
Scalar
-- hold a single value.
Composite
-- records, allow group of fields to be defined and manipulated in PL/SQL blocks.
Reference
-- hold values called pointer that designate other program items (note: not
covered in this course).
11
IBM SOLUTIONS DELIVERY INC.
Types of Variables
Some illustration of variable data types:
12
IBM SOLUTIONS DELIVERY INC.
Examples:
DECLARE
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := ‘Eastwood’;
v_comm CONSTANT NUMBER := 1400;
13
IBM SOLUTIONS DELIVERY INC.
In the syntax:
14
IBM SOLUTIONS DELIVERY INC.
Name the identifier according to the same rules used for SQL objects
Use naming conventions – for example, v_name to represent a variable and c_name to
represent a constant variable.
Declare only one identifier per line (makes code easier to read and maintain).
In constant declarations, the keyword CONSTANT must precede the type specifier.
Initialize the variable to an expression with the assignment operator (:=) or, equivalent,
with the DEFAULT reserved word.
Note: If you do not assign an initial value, the new variable contains NULL by default
until you assign a value later.
15
IBM SOLUTIONS DELIVERY INC.
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.
Example:
Wrong Correct
DECLARE DECLARE
employee_id NUMBER(6); v_employee_id NUMBER(6);
BEGIN BEGIN
SELECT employee_id SELECT employee_id
INTO employee_id INTO v_employee_id
FROM employees FROM employees
WHERE last_name = ‘Cruz’; WHERE last_name = ‘Cruz’;
END; END;
/ /
16
IBM SOLUTIONS DELIVERY INC.
DEFAULT keyword
Syntax:
identifier := expr;
Examples:
v_hiredate := ’28-JAN-2008’;
v_lastname := ‘Cruz’;
17
IBM SOLUTIONS DELIVERY INC.
Note:
If there is a single quotation mark in the string, use a single quotation mark twice.
For example:
18
IBM SOLUTIONS DELIVERY INC.
Number
Character
Date
Boolean
CHAR [(maximum_length)]
VARCHAR2 (maximum_length)
LONG
LONG RAW
NUMBER [(precesion, scale)]
BINARY_INTEGER
PLS_INTEGER
BOOLEAN
19
IBM SOLUTIONS DELIVERY INC.
VARCHAR2 Base type for variable-length character data up to 32, 767 bytes. There is no default size for
(maximum_length) VARCHAR2 variables and constants.
LONG Base type for variable-length character data up to 32, 760 bytes. Use LONG data type to
store variable-length character strings. You can insert any LONG value into a LONG
database column because the maximum width of LONG column is 2**31 bytes. However,
you cannot retrieve a value longer than 32760 bytes for a LONG column into a LONG
variable.
LONG RAW Base type for binary data and byte strings up to 32, 760 bytes. LONG RAW is not interpreted
by PL/SQL
NUMBER [(precision, Number having precision p and scale s. The precision p can range from 1 to 38. The scale s
scale)] can range from -84 to 127.
BINARY_INTEGER Base type for integers between -2, 147, 483, 647 and 2,147, 483, 647.
PLS_INTEGER Base type for signed integers between -2,147, 483,647 and 2,147, 2483, 647. PLS_INTEGER
values require less storage and are faster than NUMBER and BINARY_INTEGER values.
BOOLEAN Base type that stores one of three possible values used for logical calculations: TRUE,
FALSE, or NULL
20
IBM SOLUTIONS DELIVERY INC.
21
IBM SOLUTIONS DELIVERY INC.
22
IBM SOLUTIONS DELIVERY INC.
Rather than hard coding the data type and precision of a variable, the developer can use
the %TYPE attribute to declare a variable according to another previously declared variable
or database column.
The %TYPE attribute is most often used when the value stored in the variable will be
derived from a table in the database.
Syntax:
identifier Table.column_name%TYPE
Examples:
. . .
v_name employees.last_name%TYPE;
v_salary NUMBER(7,2);
. . .
23
IBM SOLUTIONS DELIVERY INC.
The variables are compared by the logical operators AND, OR, and NOT.
Arithmetic, character, and date expression can be use to return a Boolean value.
Examples:
24
IBM SOLUTIONS DELIVERY INC.
CLOB (character large object) data type is used to store large blocks of
single-byte character data in the database in line (inside the row) or out of
line (outside the row). (i.e. BOOK)
BLOB (binary large object) data type is use to store large binary objects in
the database in line (inside the row) or out of line (outside the row). (i.e.
PHOTO)
BFILE (binary file) data type is used to store large binary objects in
operating system files outside the database. (i.e. MOVIE)
25
IBM SOLUTIONS DELIVERY INC.
BIND Variables
A bind variable is a variable that you can declare in a host environment. Bind variables can
be used to pass run-time values, either number or character, into or out of one or more
PL/SQL programs.
To declare a bind variable in the iSQL*Plus environment, use the command VARIABLE.
For example:
26
IBM SOLUTIONS DELIVERY INC.
Example:
BEGIN
SELECT salary
INTO :g_salary
FROM employees
WHERE employee_id = 180;
END;
/
PRINT g_salary
27
IBM SOLUTIONS DELIVERY INC.
Example:
DECLARE
v_sal NUMBER(9,2) := &p_annual_sal;
BEGIN
:g_monthly_sal := v_sal/12;
END;
/
PRINT g_monthly_sal
Note: The DEFINE command specifies a user variable and assigns it a CHAR value.
28
IBM SOLUTIONS DELIVERY INC.
DBMS_OUTPUT.PUT_LINE
You have seen that you can declare a host variable, reference it in a PL/SQL block, and
then display its contents in iSQL*Plus using the PRINT command.
29
IBM SOLUTIONS DELIVERY INC.
DBMS_OUTPUT.PUT_LINE
Example:
SET SERVEROUTPUT ON
DEFINE p_annual_sal = 60000
DECLARE
v_sal NUMBER(9,2) := &pa_annual_sal;
BEGIN
v_sal := v_sal/12;
DBMS_OUTPUT.PUT_LINE (‘The monthly salary is ‘ ||
TO_CHAR(v_sal));
END;
/
30
IBM SOLUTIONS DELIVERY INC.
Questions?
31
IBM SOLUTIONS DELIVERY INC.
Practice Exercise
32
IBM SOLUTIONS DELIVERY INC.
33