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 COBOL663854-005
3-23
Numeric Data
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 (+, –
, or blank), and the last character is reserved for the null terminator. If the scale is
greater than zero, declare the char array three characters larger than the number
of digits. The extra character is reserved for the decimal point.
Within a SELECT INTO or FETCH statement, use the SQL/MX CAST function in
the select list to convert the numeric value from the database to the CHAR data
type.
Append a null terminator to the output character string before you process it as a
C character string.
See the CAST Specification in the SQL/MX Reference Manual.
Initializing DECIMAL Data Types
When initializing the DECIMAL data type, use this strcpy command, where
host_var is defined as DECIMAL:
strcpy(host_var, " 83445589");
Because the host variable is defined as DECIMAL (8,5), the length is 10 (8+2, minus
one character for the null terminator, which makes it 9).
Note. If the preprocessor -a option is used, the -n preprocessor option has no effect on
VARCHARs.