Batch Processing
The CData ODBC Driver for Teradata offers bulk load support in Teradata through the ODBC Batch API. The driver can execute related SQL data manipulation statements simultaneously by translating them into a single bulk API request. In your application's code, the driver executes statements based on an array of inputs and a parameterized query.
Batch Update Procedure
Complete the following steps to execute a batch update:
- Define arrays of column values for each parameter in the statement.
- Set the SQL_ATTR_PARAMSET_SIZE statement attribute.
- Bind each array to each parameter.
- Execute the parameterized statement.
Bulk Insert
The following code shows how to execute a bulk insert with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_INSERT_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char ProductNameArray[MAX_INSERT_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbProductNameArray[MAX_INSERT_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_INSERT_COUNT, 0);
char *ProductName1 = "Jon Doe", *ProductName2 = "Jane Doe";
cbProductNameArray[0] = strlen(ProductName1);
cbProductNameArray[1] = strlen(ProductName2);
memcpy(ProductNameArray[0], ProductName1, strlen(ProductName1));
memcpy(ProductNameArray[1], ProductName2, strlen(ProductName2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, ProductNameArray, MAX_BUFFER_SIZE, cbProductNameArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "INSERT INTO \"CData\".\"dbo\".NorthwindProducts (ProductName) VALUES (?)", SQL_NTS);
To retrieve the new records' Ids, query the LastResultInfo table:
SQLExecDirect(hstmt, (SQLTCHAR*)"SELECT * FROM LastResultInfo#TEMP", SQL_NTS);
SQLTCHAR sProductId[20] = {0};
SQLLEN cbProductId = 0;
SQLBindCol(hstmt, 1, SQL_C_CHAR, sProductId, 20, &cbProductId);
SQLRETURN retcode = SQLFetch(hstmt);
Bulk Update
An array of the primary keys of the records to update must be specified. The following code shows how to execute a bulk update with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_UPDATE_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char ProductIdArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
char ProductIdArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbProductIdArray[MAX_UPDATE_COUNT] = { 0 };
SQLLEN cbProductIdArray[MAX_UPDATE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_UPDATE_COUNT, 0);
char *ProductName1 = "Jon Doe", *ProductName2 = "Jane Doe";
char *ProductId1 = "ProductId1", *ProductId2 = "ProductId2";
cbProductNameArray[0] = strlen(ProductName1);
cbProductNameArray[1] = strlen(ProductName2);
cbProductIdArray[0] = strlen(ProductId1);
cbProductIdArray[1] = strlen(ProductId2);
memcpy(ProductNameArray[0], ProductName1, strlen(ProductName1));
memcpy(ProductNameArray[1], ProductName2, strlen(ProductName2));
memcpy(ProductIdArray[0], ProductId1, strlen(ProductId1));
memcpy(ProductIdArray[1], ProductId2, strlen(ProductId2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, ProductNameArray, MAX_BUFFER_SIZE, cbProductNameArray);
retcode = SQLBindParameter(pHstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, ProductIdArray, MAX_BUFFER_SIZE, cbProductIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "UPDATE \"CData\".\"dbo\".NorthwindProducts SET ProductName = ? WHERE ProductId = ?", SQL_NTS);
Bulk Delete
An array of the primary keys of the records to delete must be specified. The following code shows how to execute a bulk delete with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_DELETE_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char ProductIdArray[MAX_DELETE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbProductIdArray[MAX_DELETE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_DELETE_COUNT, 0);
char *ProductId1 = "ProductId1", *ProductId2 = "ProductId2";
cbProductIdArray[0] = strlen(ProductId1);
cbProductIdArray[1] = strlen(ProductId2);
memcpy(ProductIdArray[0], ProductId1, strlen(ProductId1));
memcpy(ProductIdArray[1], ProductId2, strlen(ProductId2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, ProductIdArray, MAX_BUFFER_SIZE, cbProductIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "DELETE FROM \"CData\".\"dbo\".NorthwindProducts WHERE ProductId = ?", SQL_NTS);