Declaring Program Data

Before you can make a reference to a variable constant, you must declare it. (The only exception to this rule is for the index variables of FOR loops.) All declarations must be made in the declaration section of your anonymous block, procedure, function, trigger, object type body, or package body. (See Chapter 3 for more details on the structure of the PL/SQL block and its declaration section.)

You can declare many types of data and data structures in PL/SQL, including variables, constants, TYPES (such as a type of collection or a type of record), and exceptions. This chapter focuses on the declarations of variables and constants. (See Chapter 11 for an explanation of TYPE statements. See Chapter 6 to learn how to declare exceptions.)

Declaring a Variable

When you declare a variable, PL/SQL allocates memory for the variable’s value and names the storage location so that the value can be retrieved and changed. The declaration also specifies the datatype of the variable; this datatype is then used to validate values assigned to the variable.

The basic syntax for a declaration is:

                  name datatype [NOT NULL] [default assignment];

where name is the name of the variable or constant to be declared, and datatype is the datatype or subtype that determines the type of data that can be assigned to the variable. You can include a NOT NULL clause, which means that if your code assigns a NULL to this variable, Oracle will raise an exception. The [default assignment] clause ...

Get Oracle PL/SQL Programming, Third Edition 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.