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-19
Variable-Length Character Data
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. */
void blank_pad(char *buf, size_t size);
...
int main()
{
EXEC SQL BEGIN DECLARE SECTION;
unsigned NUMERIC (4) hv_prod_num;
char hv_prod_desc[11];
EXEC SQL END DECLARE SECTION;
...
/* Initialize to blank first */
strncpy(hv_prod_desc, " ", sizeof(hv_prod_desc));
/* Then copy the initial value in */
strcpy(hv_prod_desc,"abc"); /* Copy 3 characters */
blank_pad(hv_prod_desc, sizeof(hv_prod_desc) - 1);
EXEC SQL INSERT INTO products (prod_num, prod_desc)
VALUES (:hv_prod_num, :hv_prod_desc);
...
} /* end main */
void blank_pad(char *buf, size_t size)
{
size_t i;
i = strlen(buf);
if (i < size)
memset(&buf[i], ' ', size - i);
buf[size] = '\0';
} /* end blank_pad */
Variable-Length Character Data
In a C/C++ program, you can retrieve from and insert character data into an SQL
VARCHAR column in the same way you do for a char column—by declaring a host
variable as a fixed-length character array. You can also declare a VARCHAR host
variable in an embedded SQL C/C++ program.
Note. If you are using MXCI, be sure to blank pad fixed-length character arrays, even when
inserting or updating a null value. If you do not blank pad the array, selected data in MXCI
shows misaligned data. However, the selected data in the embedded SQL program appears
correctly.
C