SQL/MX 3.2.1 Programming Manual for C and COBOL (H06.26+, J06.15+)
Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.2.1 Programming Manual for C and COBOL—663854-005
3-21
Variable-Length Character Data
The following is a declaration of the VARCHAR compatible structure:
EXEC SQL BEGIN DECLARE SECTION;
struct
{
short len;
char val[20];
}test1;
EXEC SQL END DECLARE SECTION;
The following examples use the VARCHAR compatible structure as a host variable in
the SELECT, INSERT, and UPDATE statements:
SELECT
EXEC SQL SELECT name INTO :test1 FROM t1;
INSERT
test1.len = 3;
Strcpy(test1.val,"abc");
EXEC SQL INSERT INTO t1 VALUES(:test1);
UPDATE
EXEC SQL UPDATE t1 SET name = :test1 WHERE name = 'xyz';
Generating Structures Instead of Using Null-Terminated
Strings
Prior to SQL/MX Release 1.8, all C/C++ VARCHAR columns were interpreted as null-
terminated strings. Beginning with SQL/MX Release 1.8, NonStop SQL/MX
implemented the -a preprocessor option, which translates VARCHAR host variables
into structures that contain the correct length and string fields. This behavior is similar
to invoked VARCHAR, which is generated as a structure with a length followed by a
string. If a VARCHAR structure is also a rowset, an additional array specification is part
of the structure definition.
In C/C++, the default behavior of the VARCHAR host variable type is:
VARCHAR hvar[size+1];
This host variable is translated to:
char hvar[size+1];
When you specify the -a option, the preprocessor generates this structure:
struct{
short len;










