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-15
Fixed-Length Character Data
Example
A database contains a PRODUCTS table that consists of the PROD_NUM and 
PROD_DESC columns. The product number is defined to be the primary key. This 
example selects character data and appends a null terminator to the hv_prod_desc 
array before printing the data:
EXEC SQL BEGIN DECLARE SECTION;
 unsigned NUMERIC (4) find_num;
 unsigned NUMERIC (4) hv_prod_num;
 char hv_prod_desc[11];
EXEC SQL END DECLARE SECTION;
...
EXEC SQL 
 SELECT prod_num, prod_desc INTO :hv_prod_num, :hv_prod_desc
 FROM products WHERE prod_num = :find_num;
...
/* append null terminator before displaying string */
hv_prod_desc[10] = '\0';
printf("%hu %s\n", hv_prod_num, hv_prod_desc);
Inserting or Updating Fixed-Length Character Data
Fixed-length character columns should always be padded with blanks. If the number of 
characters in an array (not including the null terminator) is less than the size of the 
character column, you must pad the array with blanks before inserting it into the 
database. Otherwise, in a C program, if the number of characters is less than the size 
of the character column, the INSERT statement stores the null terminator in the 
database, and comparison operations fail.
Examples
This example inserts data into the PRODUCTS table. The hv_prod_desc array is six 
characters long (five characters for the column value and one character for the null 
terminator). Five characters are to be inserted into the prod_desc column:
EXEC SQL BEGIN DECLARE SECTION;
unsigned NUMERIC (4) hv_prod_num;
char hv_prod_desc[6]; /* use for a 5-character column */ 
EXEC SQL END DECLARE SECTION;
...
memcpy(hv_prod_desc, "abc ", 5); /* copy 5 characters */ 
 /* (abc plus 2 blanks) */ 
hv_prod_desc[5]='\0';
...
EXEC SQL INSERT INTO products (prod_num, prod_desc) 
 VALUES (:hv_prod_num, :hv_prod_desc);
...
This example pads the hv_prod_desc array with blanks before inserting the array 
into the database and shows another way to initialize a string host variable:
/* Function to pad an array of characters with */
/* blanks on the right and add null terminator. */
C
C
C










