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 CHAR or 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, fname, and lname, based on two column definitions, FIRSTNAME, and 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[26]; char lname[26];
To use BASED ON, follow these steps:
- Use
SET DATABASEto specify the database from which column definitions are to be drawn. - Use
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;