Data retrieval in pieces in CLI applications

In CLI applications, you retrieve data in pieces because character or binary data columns can be arbitrarily long. You can call the SQLGetData() function after calling theSQLFetch() function to get the SQL_SUCCESS_WITH_INFO to indicate if more data exists for a column.

Typically, an application might choose to allocate the maximum memory the column value could occupy and bind it via SQLBindCol(), based on information about a column in the result set (obtained via a call to SQLDescribeCol(), for example, or prior knowledge).

However, in the case of character and binary data, the column can be arbitrarily long. If the length of the column value exceeds the length of the buffer the application can allocate or afford to allocate, a feature of SQLGetData() lets the application use repeated calls to obtain in sequence the value of a single column in more manageable pieces.

For example:
    /* dtlob.c */    
    /* ... */    
    sqlrc = SQLGetData(hstmt, 1, SQL_C_BINARY, (SQLPOINTER) buffer,
                    BUFSIZ, &bufInd);
    /* ... */    
    while( sqlrc == SQL_SUCCESS_WITH_INFO || sqlrc == SQL_SUCCESS )
    {   if ( bufInd > BUFSIZ)  /* full buffer */
        {   fwrite( buffer, sizeof(char), BUFSIZ, pFile);
        }
        else /* partial buffer on last GetData */
        {   fwrite( buffer, sizeof(char), bufInd, pFile);
        }

        sqlrc = SQLGetData( hstmt, 1, SQL_C_BINARY, (SQLPOINTER)buffer,
                            BUFSIZ, &bufInd);
        /* ... */	
    }

The function SQLGetSubString() can also be used to retrieve a specific portion of a large object value. For other alternative methods to retrieve long data, refer to the documentation on large object usage.