6. Embedded SQL for BASIC : BASIC Variables and Data Types : Variable Declarations : Variable and Constant Declaration Syntax
 
Share this page                  
Variable and Constant Declaration Syntax
Embedded SQL/BASIC variables and constants can be declared in a variety of ways when those declarations are in a declare section. The following sections enumerate these declaration statements and describe their syntax.
The Declare Statement
The declare statement for an Embedded SQL/BASIC variable has the following syntax:
declare type identifier [(dimensions)] {, [type] identifier [(dimensions)]}
The declare statement for an Embedded SQL/BASIC constant has the syntax:
declare type constant identifier = literal {, identifier = literal}
Syntax Notes:
If you specify the word constant, the declared constants cannot be targets of Ingres assignments.
The type must be a BASIC type acceptable to the preprocessor (see previous section) or, in the case of variables only, a record type already defined in the Embedded SQL declaration section. Note that the type is mandatory for Embedded SQL/BASIC declarations, because the preprocessor has no notion of a default type. You need only specify the type once when declaring a list of variables of the same type.
The dimensions of an array specification are not parsed by the preprocessor. Consequently, the preprocessor does not check bounds. Note also that the preprocessor will accept an illegal dimension, such as a non-numeric value, but this will later cause BASIC compiler errors.
The following example illustrates the use of the declare statement:
exec sql begin declare section
        declare integer enum, eage, string ename
        declare single constant minsal = 12496.62
        declare real esal(100)
        declare word null_ind ! Null indicator
exec sql end declare section
The Dimension Statement
The dimension statement can be used to declare arrays to the preprocessor. Its syntax is:
dimension | dim type identifier(dimensions) {, [type] identifier (dimensions)}
Syntax Notes:
The type must be a BASIC type acceptable to the preprocessor (see previous section) or a record already defined in the Embedded SQL declaration section. Note that the type is mandatory for Embedded SQL/BASIC declarations because the preprocessor has no notion of a default type. You need only specify the type once when declaring a list of variables of the same type.
The dimensions of an array specification are not parsed by the preprocessor. Consequently, the preprocessor does not check bounds. Note also that the preprocessor will accept an illegal dimension, such as a non-numeric value, but it will later cause BASIC compiler errors. Furthermore, the preprocessor does not distinguish between executable and declarative dimension statements. If you have used the dimension statement to declare an executable array to Embedded SQL/BASIC, subsequent executable dimension statements of the same array in a declaration section will cause a redeclaration error.
The following example illustrates the use of the dimension statement:
exec sql begin declare section
    dim string employee_names(100,20)
                ! declarative DIM statement
    dimension long emp_id(100,2,2)
    dimension double expenses(numdepts)
                ! executable DIM statement

exec sql end declare section
Static Storage Variable Declarations
Embedded ESQL/BASIC supports the BASIC common and map variable declarations. The syntax for a common variable declaration is as follows:
common | com [(com_name)]
              type identifier [(dimensions)] [= str_length]
              {, [type] identifier [(dimensions)] [= str_length]}
The syntax for a map variable declaration is as follows:
map | map dynamic (map_name)
              type identifier [(dimensions)] [= str_length]
              {, [type] identifier [(dimensions)] [= str_length]}
Syntax Notes:
The type must be a BASIC type acceptable to the preprocessor (see previous section) or a record type already defined to Embedded SQL/BASIC. Note that the type is mandatory for Embedded SQL/BASIC declarations because the preprocessor has no notion of a default type. You need only specify the type once when declaring a list of variables of the same type.
The dimensions of an array specification are not parsed by the preprocessor. Consequently, the preprocessor does not check bounds. Note also that the preprocessor will accept an illegal dimension, such as a non-numeric value, but it will later cause BASIC compiler errors.
The string length, if present, must be a simple integer literal.
The com_name or map_name clause is not parsed by the preprocessor. Consequently, the preprocessor will accept common and map areas of the same name in a single declaration section. It will also accept a map dynamic statement whose com_name has not appeared in another map statement. Either of these situations will later cause BASIC compiler errors.
The following example uses the common and map variable declarations:
exec sql begin declare section
    common (globals) string address = 30, integer zip
    map (ebuf) byte eage, string
                ename = 20, single emp_num
    common (globals) integer empid (200)

exec sql end declare section
The External Statement
You can inform Embedded SQL/BASIC of variables and constants declared in an external module. The syntax for a variable is as follows:
external type identifier {, identifier}
The syntax for a constant is as follows:
external type constant identifier {, identifier}
Syntax Note:
Embedded SQL/BASIC applies the same restrictions on type as VAX-11 BASIC.
exec sql begin declare section
    external integer empform, infoform
    external single constant emp_minsal

exec sql end declare section
Record Type Definitions
Embedded SQL/BASIC accepts BASIC record definitions. The syntax of a record definition is:
record identifier
               record_component
              {record_component}
end record [identifier]
where record_component can be any of the following:
type identifier [(dimensions)] [= str_length]
              {, [type] identifier [(dimensions)] [= str_length]}
group_clause
variant_clause
In turn, the syntax of a group_clause is:
group identifier [(dimensions)]
               record_component
              {record_component}
end group [identifier]
The syntax of a variant_clause is:
variant
               case_clause
              {case_clause}
end variant
where case_clause consists of:
case
              record_component
Syntax Notes:
The type must be a BASIC type acceptable to the preprocessor (see previous section) or a record type already defined in the declaration section. Note that the type is mandatory for Embedded SQL/BASIC declarations because the preprocessor has no notion of a default type. You need only specify the type once when declaring a list of variables of the same type.
Use the str_length clause only with record components of type string.
Record definitions must appear before declarations using that record type.
The following example contains record type definitions:
exec sql begin declare section
    record emp_history
            string ename = 30
            group prev_employers(10)
                string comp_name = 30
                real salary
                integer num_years
            end group prev_employers
    end record emp_history
    record emp_sports
            string ename = 30
            variant
                case
                    group golf
                        integer handicap
                        string club_name
                    end group golf
                case
                    group baseball
                        integer batting_avg
                        string team_name
                    end group baseball
                case
                    group tennis
                        integer seed
                        string club_name
                    end group tennis
            end variant
    end record emp_sports

exec sql end declare section
Indicator Variables
An indicator variable is a 2-byte integer variable. There are three possible ways to use them in an application:
In a statement that retrieves data from Ingres, you can use an indicator variable to determine if its associated host variable was assigned a null value.
In a statement that sets data to Ingres, you can use an indicator variable to assign a null to the database column.
In a statement that retrieves character data from Ingres, you can use the indicator variable as a check that the associated host variable was large enough to hold the full length of the returned character string. You can use SQLSTATE to do this. Although you can use SQLCODE as well, it is preferable to use SQLSTATE because SQLCODE is a deprecated feature.
You can declare an indicator using the integer word subtype or, if you used the -i2 preprocessor command line flag, you can declare an indicator as an integer. The following example declares two indicator variables, one a single variable and the other an array of indicators:
declare word ind, ind_arr(10)
When using an indicator variable with a BASIC record, you must declare the indicator variable as an array of 2-byte integers. In the above example, you can use the variable "ind_arr" as an indicator array with a record assignment.