Use Data-at-Execution Columns (ODBC)
To use data-at-execution text, ntext, or image columns
For each data-at-execution column, put special values into the buffers previously bound by SQLBindCol:
For the last parameter, use SQL_LEN_DATA_AT_EXEC(length) where length is the total length of the text, ntext, or image column data in bytes.
For the fourth parameter, put a program-defined column identifier.
Calling SQLSetPos returns SQL_NEED_DATA, which indicates that data-at-execution columns are ready for processing.
For each data-at-execution column:
Call SQLParamData to get the column array pointer. It will return SQL_NEED_DATA if there is another data-at-execution column.
Call SQLPutData one or more times to send the column data, until length is sent.
Call SQLParamData to indicate that all the data for the final data-at-execution column is sent. It will not return SQL_NEED_DATA.
Example
The sample shows how to read a SQL_LONG variable character data using SQLGetData. This sample is not supported on IA64.
You will need an ODBC data source called AdventureWorks, whose default database is the AdventureWorks sample database. (You can download the AdventureWorks sample database from the Microsoft SQL Server Samples and Community Projects home page.) This data source must be based on the ODBC driver that is supplied by the operating system (the driver name is "SQL Server"). If you will build and run this sample as a 32-bit application on a 64-bit operating system, you must create the ODBC data source with the ODBC Administrator in %windir%\SysWOW64\odbcad32.exe.
This sample connects to your computer's default SQL Server instance. To connect to a named instance, change the definition of the ODBC data source to specify the instance using the following format: server\namedinstance. By default, SQL Server Express installs to a named instance.
Execute the first (Transact-SQL) code listing to create the table used by the sample.
Compile the second (C++) code listing with odbc32.lib. Then execute the program.
Execute the third (Transact-SQL) code listing to delete the table used by the sample.
use AdventureWorks
CREATE TABLE emp3 (NAME char(30), AGE int, BIRTHDAY datetime, Memo1 text)
INSERT INTO emp3 (NAME, AGE, Memo1) VALUES ('Name1', '12', 'This is the first employee')
INSERT INTO emp3 (NAME, AGE, Memo1) VALUES ('Name2', '18', 'This is the second employee')
// compile with: odbc32.lib
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>
#define BUFFERSIZE 450
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
void Cleanup() {
if (hstmt1 != SQL_NULL_HSTMT)
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
if (hdbc1 != SQL_NULL_HDBC) {
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
}
if (henv != SQL_NULL_HENV)
SQLFreeHandle(SQL_HANDLE_ENV, henv);
};
int main() {
RETCODE retcode;
SWORD cntr;
// SQLGetData variables.
UCHAR Data[BUFFERSIZE];
SDWORD cbBatch = (SDWORD)sizeof(Data)-1;
SQLLEN cbTxtSize;
// Clear data array.
for (cntr = 0 ; cntr < BUFFERSIZE ; cntr++)
Data[cntr] = 0x00;
// Allocate the ODBC environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
if( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLAllocHandle(Env) Failed\n\n");
Cleanup();
return(9);
}
// Notify ODBC that this is an ODBC 3.0 app.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLSetEnvAttr(ODBC version) Failed\n\n");
Cleanup();
return(9);
}
// Allocate ODBC connection handle and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLAllocHandle(hdbc1) Failed\n\n");
Cleanup();
return(9);
}
// Sample uses Integrated Security, create SQL Server DSN using Windows NT authentication.
retcode = SQLConnect(hdbc1, (UCHAR*)"AdventureWorks", SQL_NTS, (UCHAR*)"", SQL_NTS, (UCHAR*)"", SQL_NTS);
if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("SQLConnect() Failed\n\n");
Cleanup();
return(9);
}
// Allocate statement handle; prepare, then execute command.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("SQLAllocHandle(hstmt1) Failed\n\n");
Cleanup();
return(9);
}
retcode = SQLExecDirect(hstmt1, (UCHAR*)"SELECT Memo1 FROM emp3", SQL_NTS);
if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("SQLExecDirect Failed\n\n");
Cleanup();
return(9);
}
// Get first row.
retcode = SQLFetch(hstmt1);
if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("SQLFetch(hstmt1) Failed\n\n");
Cleanup();
return(9);
}
// Get the SQL_LONG column.
cntr = 1;
while ( (retcode = SQLGetData(hstmt1, 1, SQL_C_CHAR, Data, cbBatch, &cbTxtSize)) != SQL_NO_DATA) {
printf("GetData iteration %d, pcbValue = %d,\n", cntr++, cbTxtSize);
printf("Data = %s\n\n", Data);
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("GetData(hstmt1) Failed\n\n");
Cleanup();
return(9);
}
}
// Clean up
//SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
use AdventureWorks
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'emp3')
DROP TABLE emp3
GO