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-19
Numeric Data
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).
Initializing NUMERIC Data Types
Initialize NUMERIC data types in your application with an assignment statement. No
strcpy or CAST is needed. NUMERIC types are binary types. In this example, the
value 10 is moved to host_var, which is declared as a long.
EXEC SQL BEGIN DECLARE SECTION;
NUMERIC(7,0) host_var;
EXEC SQL END DECLARE SECTION;
host_var=10;
Storing C Character Arrays Into SQL Numeric Columns
You can use C character arrays as host variables in a C program when inserting or
updating values into numeric columns.
When you use char arrays as host variables for NUMERIC, DECIMAL, or PICTURE
9’s columns, use the SQL/MX CAST function to convert the value:
•
Format a C character string, without the null terminator, consisting of a sign, digits,
and a decimal point. The formatted C character string must be left-justified and
padded with blanks within the character array.