CreateQueryDef Method

See Also      Applies To

Creates a new CdbQueryDef object in a specified CdbConnection or CdbDatabase object.

Syntax

CdbQueryDefCreateQueryDef(LPCTSTR pstrName = NULL,

LPCTSTR pstrSQL = NULL);

Parameters

Type Example Description
LPCTSTR pstrName Optional. A pointer to a string that uniquely names the new CdbQueryDef.
LPCTSTR pstrSQL Optional. A pointer to a string that is an SQL statement defining the CdbQueryDef. If you omit this argument, you can define the CdbQueryDef by setting its SQL property before or after you append it to a collection.

Remarks

In a Microsoft Jet workspace, if you provide anything other than a zero-length string for the name when you create a CdbQueryDef, the resulting CdbQueryDef object is automatically appended to the QueryDefs collection. In an ODBCDirect workspace, CdbQueryDef objects are always temporary.

In an ODBCDirect workspace, the pstrSQL argument can specify an SQL statement or a Microsoft SQL Server stored procedure and its parameters.

If the object specified by pstrName is already a member of the QueryDefs collection, a run-time error occurs. You can create a temporary CdbQueryDef by using a zero-length string for the pstrName argument when you execute the CreateQueryDef method. You can also accomplish this by setting the Name property of a newly created CdbQueryDef to a zero-length string (""). Temporary CdbQueryDef objects are useful if you want to repeatedly use dynamic SQL statements without having to create any new permanent objects in the QueryDefs collection. You can't append a temporary CdbQueryDef to any collection because a zero-length string isn't a valid name for a permanent CdbQueryDef object. You can always set the Name and SQL properties of the newly created CdbQueryDef object and subsequently append the CdbQueryDef to the QueryDefs collection.

To run the SQL statement in a CdbQueryDef object, use the Execute or OpenRecordset method.

Using a QueryDef object is the preferred way to perform SQL pass-through queries with ODBC databases.

To remove a CdbQueryDef object from a QueryDefs collection in a Microsoft Jet database, use the Delete method on the collection. For an ODBCDirect database, use the Close method on the CdbQueryDef object.

Usage

#include <afxole.h>
#include <dbdao.h>

CdbDatabase   db;
CdbQueryDef   qd1;
...                        // Initialize db, etc.
qd1 = CreateQueryDef(_T("QD_PARTS"), _T("SELECT * FROM PARTS"));