Using BASED ON to Declare Variables
Go Up to Declaring Host Variables
InterBase supports a declarative clause,
BASED ON, for creating C language character variables based on column definitions in a database. Using
BASED ON ensures that the resulting host-language variable is large enough to hold the maximum number of characters in a
VARCHAR database column, plus an extra byte for the null-terminating character expected by most C string functions.
BASED ON uses the following syntax:
BASED ON <dbcolumn> hostvar;
For example, the following statements declare two host variables,
lname, based on two column definitions,
LASTNAME, in an employee database:
BASED ON EMP.FIRSTNAME fname; BASED ON EMP.LASTNAME lname;
Embedded in a C or C++ program, these statements generate the following host- variable declarations during preprocessing:
char fname; char lname;
BASED ON, follow these steps:
SET DATABASEto specify the database from which column definitions are to be drawn.
CONNECTto attach to the database.
- Declare a section with
BEGIN DECLARE SECTION.
- Use the
BASED ONstatement to declare a string variable of the appropriate type.
The following statements show the previous
BASED ON declarations in context:
EXEC SQL SET DATABASE EMP = 'employee.ib'; EXEC SQL CONNECT EMP; EXEC SQL BEGIN DECLARE SECTION; int empno; BASED ON EMP.FIRSTNAME fname; BASED ON EMP.LASTNAME lname; EXEC SQL END DECLARE SECTION;