Variable Declaration
Syntax
variable_declaration ::= DECLARE (variable_name
type_definition
";")+
variable_name ::= "$" id
External Variables
A query may start with a variable declaration section. The variables declared here are called external variables. The value of an external variable is global and constant. The values of external variables are not known in advance when the query is formulated or compiled. Instead, the external variables must be bound to their actual values before the query is executed. This is done via programmatic APIs. See Java Direct Driver Developer's Guide.
The type of the item bound to an external variable must be equal to or a subtype of the variable's declared type. The use of external variables allows the same query to be compiled once and then executed multiple times, with different values for the external variables each time. All the external variables that appear in a query must be declared in the declaration section. This is because knowing the type of each external variable in advance is important for query optimization.
Note:
External variables play the role of the global constant variables found in traditional programming languages (e.g. final static variables in java, or const static variables in c++).Internal Variables
Oracle NoSQL Database allows implicit declaration of internal variables as well. Internal variables are bound to their values during the execution of the expressions that declare them.
Variables (internal and external) can be referenced in other expressions by their name. In fact, variable references themselves are expressions, and together with literals, are the starting building blocks for forming more complex expressions.
Scope
Each variable is visible (i.e., can be referenced) within a scope. The query as a whole defines the global scope, and external variables exist within this global scope. Certain expressions create sub-scopes. As a result, scopes may be nested. A variable declared in an inner scope hides another variable with the same name that is declared in an outer scope. Otherwise, within any given scope, all variable names must be unique.
Note:
The names of variables are case-sensitive.Note:
The following variable names cannot be used as names for external variables: $key, $value, $element, and $pos.Example 6-4 Variable Declaration
DECLARE $age INTEGER;
SELECT firstName, lastName
FROM Users
WHERE age > $age