Executing statements directly

The ICommandText::Execute() function prepares and executes a SQL statement. Optionally, the statement can include parameters.

The following code fragment illustrates how to execute a statement without parameters.

Obtain a Command object from the session:

ICommandText* pICommandText;
hr = pIDBCreateCommand->CreateCommand(
     NULL, IID_ICommandText,
     (IUnknown**)&pICommandText);

Set the SQL statement the command will execute:

hr = pICommandText->SetCommandText(
     DBGUID_DBSQL,
     L"DELETE FROM publishers where pub_id = '7777' ");

Execute the command. The cRowsAffected contain the number of rows inserted, deleted, or updated by the command. The pIRowset is assigned to the Rowset object created by the command, as shown:

DBROWCOUNT cRowsAffected;
IRowset* pIRowset;
hr = pICommandText->Execute(
     NULL, IID_IRowset, NULL,
     &cRowsAffected, (IUnknown**)&pIRowset);