SQL/MP Programming Manual for C
Host Variables
HP NonStop SQL/MP Programming Manual for C—429847-008
2-8
Fixed-Length Character Data
SHIPMENTS table and appends a null terminator to the prod_desc array before
printing the data:
EXEC SQL BEGIN DECLARE SECTION;
short prod_num;
char prod_desc[11];
EXEC SQL END DECLARE SECTION;
...
EXEC SQL
SELECT prod_num, prod_desc INTO :prod_num, :prod_desc
FROM =shipments WHERE prod_num > min_num;
...
/* append null terminator before displaying string */
prod_desc[11] = "\0";
printf("%d %s\n", prod_num, prod_desc);
Inserting Character Data
In an SQL/MP database, fixed-length character columns are always padded with
blanks. Therefore, if the number of characters in an array is less than the size of the
character column, pad the array with blanks before inserting it into the database.
Otherwise, the INSERT statement stores the null terminator in the database, and
comparison operations fail. This example inserts data into the PRODUCTS table. The
prod_desc array is six bytes long (five byes for the column value and one byte for the
null terminator).
void function(void)
{
EXEC SQL BEGIN DECLARE SECTION;
char prod_desc[6]; /* Use for a 5-character column */
EXEC SQL END DECLARE SECTION;
memcpy(prod_desc, "abc ", 5); /* copy 5 characters */
/* (abc plus 2 blanks) */
...
EXEC SQL INSERT INTO =products VALUES (:prod_desc);
}
This example pads the prod_desc array with blanks before it inserts the array into the
database:
/* Routine to pad an array of characters */
/* with blanks on the right. */
void blank_pad(char *buf, size_t size)
{
size_t i;
i = strlen(buf);
if (i < size)
memset(&buf[i], ' ', size - i);
}
void function(void)
{
EXEC SQL BEGIN DECLARE SECTION;