You can declare a variable or constant to allocate storage space and name the storage location for later reference.
You must declare an object before you can reference it. A declaration can appear in the declaration section of any block, subprogram, or package.
Applicability
This section applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL mode.
NOT NULL constraint
You can apply the NOT NULL constraint to a scalar variable or constant, or a scalar component of a composite variable or constant.
The NOT NULL constraint prevents a null value from being assigned to the object. The NOT NULL attribute of an object can be implicitly or explicitly specified.
If you specify the NOT NULL constraint for a scalar variable during its declaration, you must initialize the variable (because the default initial value of a scalar variable is NULL).
PL treats any zero-length string as a NULL value, including the return values of character functions and BOOLEAN expressions.
Declare a variable
When you declare a variable, you must specify the name and data type of the variable. You can also specify an initial value for the variable.
The name of the variable must be a valid user-defined identifier, and the data type can be any PL data type. PL data types include all SQL data types. The data type can be a scalar type or a composite type.
Declare a constant
A constant is used to store an invariant value.
The information specified in a variable declaration also applies to a constant declaration, but a constant declaration requires the CONSTANT keyword and the initial value of the constant (which is also the permanent value of the constant).
Initial values of variables and constants
In a variable declaration, the initial value is optional unless the NOT NULL constraint is specified. In a constant declaration, the initial value is mandatory.
If the declaration is in a block or subprogram, the initial value is assigned to the variable or constant each time the block or subprogram is executed. If the declaration is in the SPECIFICATION section of a package, the initial value is assigned to the variable or constant in each session, regardless of whether the variable or constant is public or private.
You can use the assignment operator (:=) or the DEFAULT keyword followed by an expression to specify an initial value. The expression can contain the initial values of previously declared constants and the initial values or currently assigned values of previously initialized variables. If an initial value is not specified for a variable, you must assign a value to the variable before you use it.
Here is an example:
obclient> DECLARE
pi CONSTANT NUMBER := 3.14159;
radius NUMBER NOT NULL := 1;
area NUMBER := (pi * radius**2);
BEGIN
DBMS_OUTPUT.PUT_LINE('The area of the circle is: '||area);
END;
/
Query OK, 0 rows affected
The area of the circle is: 3.14159
Declare using %TYPE
You can declare an object that has the same data type as a previously declared variable or column without knowing the data type.
The advantages of using the %TYPE attribute are as follows:
The data type of the referenced database column can be unknown.
The variable type will change as the data type of the referenced column changes.
The syntax is as follows:
referencing_item referenced_item%TYPE;
The referenced item will be inherited by the referencing item:
Data type and size
Constraints (excluding those on columns)
The referenced item does not inherit the initial value of the referencing item. Therefore, if the referencing item specifies or inherits the NOT NULL constraint, you must specify an initial value for it.
%TYPE is particularly useful for declaring variables to store database values. The syntax for declaring a variable that has the same data type as a column is as follows:
variable_name table_name.column_name%TYPE;
Here is an example:
obclient> DECLARE
firstname VARCHAR(30) NOT NULL := 'San';
lastname firstname%TYPE := 'Zhang';
BEGIN
DBMS_OUTPUT.PUT_LINE('firstname=' || firstname);
DBMS_OUTPUT.PUT_LINE('lastname=' || lastname);
END;
/
Query OK, 0 rows affected
firstname=San
lastname=Zhang
In this example, the lastname variable inherits the data type, size, and NOT NULL constraint of the firstname variable. Since the lastname variable does not inherit the initial value of the firstname variable, an initial value must be specified for it (no more than 30 characters).