This article was previously published under Q235053
On This Page
SYMPTOMS
Using the OLE DB Provider for SQL Server and calling the ATL CCommand::Prepare() function, or ICommandPrepare::Prepare(), returns E_FAIL with the following error description:
Syntax error or access violation.
The SQL Server OLE DB Provider that ships with MDAC 2.7 returns the following improved error message:
Parameter Information cannot be derived from SQL statements with sub-select queries. Set parameter information before preparing command.
You must call CCommand::SetParameterInfo() or ICommandWithParameters::SetParameterInfo() before calling Prepare().
Please see the MORE INFORMATION section of this article for sample code that uses the ATL OLE DB consumer templates and demonstrates calling SetParameterInfo().
Create two tables on a Microsoft SQL Server database: Table1 should have a character field and an integer field, Table2 should have an integer field only.
2.
Create an accessor class that resembles the following:
class CQuery1Accessor
{
public:
TCHAR m_field1[11];
LONG m_field2;
LONG m_field1param;
BEGIN_COLUMN_MAP(CQuery1Accessor)
COLUMN_ENTRY(1, m_field1)
COLUMN_ENTRY(2, m_field2)
END_COLUMN_MAP()
BEGIN_PARAM_MAP(CQuery1Accessor)
COLUMN_ENTRY(1,m_field1param)
END_PARAM_MAP()
DEFINE_COMMAND(CQuery1Accessor, _T("SELECT * FROM Table1 WHERE field2 IN"
"(SELECT field1 FROM Table2 where field1 > ?)"));
// You may want to call this function if you are inserting a record and want to
// initialize all the fields, if you are not going to explicitly set all of them.
void ClearRecord()
{
memset(this, 0, sizeof(*this));
}
};
3.
Create a CCommand class that executes the command using the accessor class you just created. The CCommand class should resemble the following:
Contact Microsoft Phone Numbers, Support Options and Pricing, Online Help, and more.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.