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-22
Variable-Length Character Data
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;
...
strcpy(hv_prod_desc, "abc"); /* Copy 3 characters */
hv_prod_desc[3]='\0';
...
EXEC SQL INSERT INTO products (prod_num, prod_desc)
VALUES (:hv_prod_num, :hv_prod_desc);
...
In contrast to char data, for VARCHAR data, you do not need to insert blanks following
the data up to the null terminator.
Example: Using a Structure
This example is the same as the previous one except that the preprocessor option -a
is used. The preprocessor -a option specifies that C/C++ interpret VARCHAR host
variables as structures and generate structures that contain the correct length and
string fields. For details, see Generating Structures Instead of Using Null-Terminated
Strings on page 3-21.
If you use the -a option, you must specify the value (val) and length (len) of the
structure when assigning data to the host variable.
EXEC SQL BEGIN DECLARE SECTION;
unsigned NUMERIC (4) hv_prod_num;
VARCHAR hv_prod_desc[11];
EXEC SQL END DECLARE SECTION;
...
strncpy(hv_prod_desc.val, "abc", 3); /* Copy 3 characters */
hv_prod_desc.len = 3;
...
EXEC SQL INSERT INTO products (prod_num, prod_desc)
C
C










