The DECLARE statement defines global and local variables for use as value expressions. When DEC DATATRIEVE initializes a variable, it assigns the variable the MISSING VALUE or DEFAULT VALUE if one is specified in the DECLARE statement. DEC DATATRIEVE assigns a value of zero to numeric variables and a space to alphabetic and alphanumeric variables if no MISSING VALUE or DEFAULT VALUE is specified.
1 – Global Variables
You can define global variables only with DECLARE statements entered in response to the DTR> prompt of DEC DATATRIEVE command level. You can use a global variable as a value expression in any DEC DATATRIEVE statement. Unless you define a global variable with a COMPUTED BY clause, the global variable retains the value you assign to it until you either assign it a new value or release it with the RELEASE command. The value of a global variable defined with a COMPUTED BY clause depends on the value expression that controls the computation. For example, you can declare the value of the variable to be 1.2 times the price of a boat in the YACHTS domain. The value of the variable changes according to the value of the PRICE field for different records: DTR> READY YACHTS DTR> DECLARE VAR COMPUTED BY PRICE * 1.2. DTR> FOR FIRST 5 YACHTS PRINT VAR USING $$$,$$$.99 VAR $44,341.20 $21,480.00 $33,000.00 $22,320.00 $11,874.00 DTR>
2 – Local Variables
You can define local variables with DECLARE statements entered in BEGIN-END and THEN statements. A local variable is released as soon as DEC DATATRIEVE completes the execution of the clause or statement in which it was declared. Although a local variable stays in effect for subsequent statements of the compound statement in which it is declared, it has no meaning in any outer statements containing that compound statement.
3 – Null Values and Variables
DEC DATATRIEVE supports relational databases null values in variables. Therefore, DEC DATATRIEVE has the following behavior: o When transferring a missing value from a DEC DATATRIEVE variable to a relational field, the field receives a null value. DTR> DECLARE VAR PIC X(10) MISSING VALUE "77". DTR> PRINT VAR VAR 77 DTR> STORE EMPLOYEE_MANAGER_TABLE USING EMP_ID = VAR DTR> PRINT EMPLOYEE_MANAGER_TABLE EMP_ID MGR_ID NULL NULL DTR> o When transferring a null value from a relational field to a DEC DATATRIEVE variable, the variable receives either its missing value (if a MISSING VALUE clause is specified in the variable definition), or a null value (if the variable does not contain a MISSING VALUE clause). DTR> DECLARE EMPLOYEE PIC X(10) MISSING VALUE 8999. DTR> FOR X IN EMPLOYEE_MANAGER_TABLE CON> EMPLOYEE = EMP_ID DTR> PRINT EMPLOYEE EMPLOYEE 8999 DTR> DECLARE MANAGER PIC X(10). DTR> FOR X IN EMPLOYEE_MANAGER_TABLE CON> MANAGER = MGR_ID DTR> PRINT MANAGER MANAGER NULL DTR>