Null terminated strings in C and C++ embedded SQL applications
C and C++ null-terminated
strings have their own SQLTYPE (460/461 for character and 468/469
for graphic).
C and C++ null-terminated strings are handled differently, depending
on the value of the LANGLEVEL precompiler option. If a host variable
of one of these SQLTYPE values and declared length n is specified
within an SQL statement, and the number of bytes (for character types)
or double-byte characters (for graphic types) of data is k,
then:
- If the LANGLEVEL option on the PREP command is SAA1 (the default):
- For Output:
-
- If...
- Then...
- k > n
- n characters are moved to the target host variable, SQLWARN1 is set to 'W', and SQLCODE 0 (SQLSTATE 01004). No null-terminator is placed in the string. If an indicator variable was specified with the host variable, the value of the indicator variable is set to k.
- k = n
- k characters are moved to the target host variable, SQLWARN1 is set to 'N', and SQLCODE 0 (SQLSTATE 01004). No null-terminator is placed in the string. If an indicator variable was specified with the host variable, the value of the indicator variable is set to 0.
- k < n
- k characters are moved to the target host variable and a null character is placed in character k + 1. If an indicator variable was specified with the host variable, the value of the indicator variable is set to 0.
- For input:
- When the database manager encounters an input host variable of one of these SQLTYPE values that does not end with a null-terminator, it will assume that character n+1 will contain the null-terminator character.
- If the LANGLEVEL option on the PREP command is MIA:
- For output:
-
- If...
- Then...
- k >= n
- n - 1 characters are moved to the target host variable, SQLWARN1 is set to 'W', and SQLCODE 0 (SQLSTATE 01501). The nth character is set to the null-terminator. If an indicator variable was specified with the host variable, the value of the indicator variable is set to k.
- k + 1 = n
- k characters are moved to the target host variable, and the null-terminator is placed in character n. If an indicator variable was specified with the host variable, the value of the indicator variable is set to 0.
- k + 1 < n
- k characters are moved to the target host variable, n - k -1 blanks are appended on the right starting at character k + 1, then the null-terminator is placed in character n. If an indicator variable was specified with the host variable, the value of the indicator variable is set to 0.
- For input:
- When the database manager encounters an input host variable of one of these SQLTYPE values that does not end with a null character, SQLCODE -302 (SQLSTATE 22501) is returned.
As previously defined, when specified in any other SQL context, a host variable of SQLTYPE 460 with length n is treated as a VARCHAR data type with length n and a host variable of SQLTYPE 468 with length n is treated as a VARGRAPHIC data type with length n.