SQL/MX 3.2 Programming Manual for C and COBOL (H06.25+, J06.14+)
Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.2 Programming Manual for C and COBOL—663854-002
3-18
Variable-Length Character Data
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;
char val[size+1];
} hvar;
Inserting or Updating Variable-Length Character Data
The rules for selecting and inserting or updating variable-length character data are
similar to the rules for fixed-length character data with one exception. When inserting
or updating data with VARCHAR data type, you do not have to pad the array with
blanks.
Example: Using a Null-Terminated String
Using the SQL/MX default for VARCHAR (for example, null terminated string), this
example uses a VARCHAR declaration for an SQL column up to 11 characters in length:
EXEC SQL BEGIN DECLARE SECTION;
unsigned NUMERIC (4) hv_prod_num;
VARCHAR hv_prod_desc[11];
EXEC SQL END DECLARE SECTION;
...
C










