Batch Processing
The CData ODBC Driver for Microsoft Dynamics 365 Business Central offers bulk load support in Microsoft Dynamics 365 Business Central 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 NameArray[MAX_INSERT_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbNameArray[MAX_INSERT_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_INSERT_COUNT, 0);
char *Name1 = "Jon Doe", *Name2 = "John";
cbNameArray[0] = strlen(Name1);
cbNameArray[1] = strlen(Name2);
memcpy(NameArray[0], Name1, strlen(Name1));
memcpy(NameArray[1], Name2, strlen(Name2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, NameArray, MAX_BUFFER_SIZE, cbNameArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "INSERT INTO Accounts (Name) VALUES (?)", SQL_NTS);
To retrieve the new records' Ids, query the LastResultInfo table:
SQLExecDirect(hstmt, (SQLTCHAR*)"SELECT * FROM LastResultInfo#TEMP", SQL_NTS);
SQLTCHAR sAccountId[20] = {0};
SQLLEN cbAccountId = 0;
SQLBindCol(hstmt, 1, SQL_C_CHAR, sAccountId, 20, &cbAccountId);
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 accountidArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
char AccountIdArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbaccountidArray[MAX_UPDATE_COUNT] = { 0 };
SQLLEN cbAccountIdArray[MAX_UPDATE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_UPDATE_COUNT, 0);
char *Name1 = "Jon Doe", *Name2 = "John";
char *AccountId1 = "AccountId1", *AccountId2 = "AccountId2";
cbNameArray[0] = strlen(Name1);
cbNameArray[1] = strlen(Name2);
cbAccountIdArray[0] = strlen(AccountId1);
cbAccountIdArray[1] = strlen(AccountId2);
memcpy(NameArray[0], Name1, strlen(Name1));
memcpy(NameArray[1], Name2, strlen(Name2));
memcpy(AccountIdArray[0], AccountId1, strlen(AccountId1));
memcpy(AccountIdArray[1], AccountId2, strlen(AccountId2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, NameArray, MAX_BUFFER_SIZE, cbNameArray);
retcode = SQLBindParameter(pHstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, AccountIdArray, MAX_BUFFER_SIZE, cbAccountIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "UPDATE Accounts SET Name = ? WHERE AccountId = ?", 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 AccountIdArray[MAX_DELETE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbAccountIdArray[MAX_DELETE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_DELETE_COUNT, 0);
char *AccountId1 = "AccountId1", *AccountId2 = "AccountId2";
cbAccountIdArray[0] = strlen(AccountId1);
cbAccountIdArray[1] = strlen(AccountId2);
memcpy(AccountIdArray[0], AccountId1, strlen(AccountId1));
memcpy(AccountIdArray[1], AccountId2, strlen(AccountId2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, AccountIdArray, MAX_BUFFER_SIZE, cbAccountIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "DELETE FROM Accounts WHERE AccountId = ?", SQL_NTS);