SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Host Variables in C/C++ Programs
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
3-17
Variable-Length Character Data
Example
This example uses a declaration for an SQL column up to 20 characters in length:
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR last_name[21]; /* 20-character last name */
EXEC SQL END DECLARE SECTION;
...
Follow the guidelines outlined in Fixed-Length Character Data on page 3-14 for
handling the null terminator when you declare and use VARCHAR arrays as host
variables for variable-length string literals.
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
C