SQL/MX 3.1 Programming Manual for C and COBOL (H06.23+, J06.12+)

Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.1 Programming Manual for C and COBOL663854-001
3-19
Numeric Data
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-18.
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.
When you use char arrays as host variables for NUMERIC, DECIMAL, or PICTURE
9’s data, use the SQL/MX CAST function to convert the value:
If you are converting NUMERIC, PICTURE 9’s, or DECIMAL data to a C char
array and the scale is zero, declare the char array two characters larger than the
number of digits you expect to store in the array. The first character is the sign (+, –
Note. If the preprocessor -a option is used, the -n preprocessor option has no effect on
VARCHARs.
C