Data Length, Buffer Length, and Truncation
For fixed-length data, such as an integer or a date structure, the byte length
of the data is always the size of the data type. In general, applications
allocate a data buffer that is the size of the data type. If the application
allocates a smaller buffer, the consequences are undefined as the driver assumes the
data buffer is the size of the data type and does not truncate the data to fit
into a smaller buffer. If the application allocates a larger buffer, the extra
space is never used.
For variable-length data, such as character or binary data, it is important to
recognize that the byte length of the data is separate from and often
different than the byte length of the buffer. The relation of these two lengths is
shown in the “Buffers” section, earlier in this chapter. If the byte length of the data is greater
than the byte length of the buffer, the driver truncates data being fetched to
the byte length of the buffer and returns SQL_SUCCESS_WITH_INFO with SQLSTATE
01004 (Data truncated). However, the returned byte length is the length of the
untruncated data.
For example, suppose an application allocates 50 bytes for a binary data
buffer. If the driver has 10 bytes of binary data to return, it returns those 10
bytes in the buffer. The byte length of the data is 10, and the byte length of the
buffer is 50. If the driver has 60 bytes of binary data to return, it
truncates the data to 50 bytes, returns those bytes in the buffer and returns
SQL_SUCCESS_WITH_INFO. The byte length of the data is 60 (the length before truncation),
and the byte length of the buffer is still 50.
A diagnostic record is created for each column that is truncated. Because it
takes time for the driver to create these records and for the application to
process them, truncation can degrade performance. Usually, an application can
avoid this problem by allocating large enough buffers, although this might not be
possible when working with long data. When data truncation occurs, the
application can sometimes allocate a larger buffer and refetch the data; this is not
true in all cases. If truncation occurs while getting data with calls to SQLGetData, the application need not call SQLGetData for data that has already been returned; for more information, see “Getting Long Data” in Chapter 10, “Retrieving Results (Basic).”