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 COBOL523627-004
3-18
Numeric Data
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-17.
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)
VALUES (:hv_prod_num, :hv_prod_desc);
...
Numeric Data
Use the NUMERIC data type for fixed-point numeric data, the DECIMAL data type for
ASCII numeric data, and the PICTURE 9’s data type for either fixed-point (COMP) or
ASCII (DISPLAY) numeric data.
Assigning SQL Numeric Data to C Character Arrays
Any of the SQL numeric data types can be assigned to a host variable with char data
type in a C program by first performing the appropriate conversion.
Note. If the preprocessor -a option is used, the -n preprocessor option has no effect on
VARCHARs.
C
C