2.4. Variables

In PL/SQL, before you can work with any kind of variable, you must first declare it; that is, you must give it a name and specify its datatype. Borrowing experts' terminology, you could restate this as "PL/SQL is a strongly typed programming language." Whatever you call it, this section reviews common PL/SQL datatypes and discusses how to declare variables.

2.4.1. Datatypes

The most common datatypes in PL/SQL are in four families: string, number, date, and logical (Boolean).

2.4.1.1. Strings

Strings are "free form" data. A string can contain any valid character in the character set of a language. While there are several variations of strings, the datatype you will almost always use is VARCHAR2.

VARCHAR2 variables store variable-length character strings, which means that the length of the string depends on the value stored in the variable (which can vary). When you declare a variable-length string, you must also specify a maximum length for the string, which can range from 1 to 32,767 bytes. The general format for a VARCHAR2 declaration is:

variable_name VARCHAR2(n);

Where:

variable_name

Programmer-supplied identifier that is subject to PL/SQL's naming rules (see Section 2.9.2 near the end of the chapter).

n

Literal integer between 1 and 32,767 that designates the maximum length of the string's contents, expressed by default in bytes.[2]

[2] Some languages need more than one byte per character, so Oracle9i introduced a way to declare variables in terms ...

Get Learning Oracle PL/SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.