The Query object gives an opportunity to build a query that is a user's view of GDB tables (including temporary tables) and external database tables. This version of the GIS kernel does not provide an opportunity to create mixed queries based on temporary and persistent GDB tables. To access records contained in the query, the Query::OpenRecordset method should be used. It creates the Recordset object representing a set of records that satisfy query conditions.
To create a new query, the following steps should be performed:
Use the GeoDatabase::CreateQuery method to create a query (the Query object) based on GDB tables. Use the ExternalDatabase::CreateQuery method to create a query (the Query object) based on external database tables. A table being an argument of these methods is the root table of the query.
Joining base tables in a query is performed by the Field::Join method. Not all external databases support using the same table two or more times in a join. To determine this, use the ExternalDatabase::CanMultiJoinTable property. For queries based on GDB tables, it is impossible to use a table two or more times in a join.
Setting reference tables for query fields is performed by the Field::SetReference method.
To specify search conditions, the FieldSearchCondition objects must be created by the Field::CreateSearchCondition method. These objects are search conditions on specified query fields. The search conditions must be added to the Query::FieldSearchConditions collection by the Append method after they were set up. You can change or set search conditions by setting up the current conditions in the Query::FieldSearchConditions collection.
Determining calculated fields is performed by the Field::Calculate method. Dividing records into groups, upon which values of such fields are calculated, is performed taking into account visible, non-calculated fields of a query. Not all external databases support an opportunity to determine calculated fields. To check this, use the ExternalDatabase::SupportCalculations property. For queries based on GDB tables, it is always possible to determine calculated fields.
Setting the Query::Duplicate property can prevent duplicate records.
To specify a sort condition for a query, the FieldSortCondition objects must be created by the Field::CreateSortCondition method. These objects are sort conditions on a specified field. Created conditions must be added to the Query::FieldSortConditions collection. The order of sort conditions in the collection specifies the priority for sorting by fields.
It is possible to create a new query by specifying an SQL statement SELECT for queries built on tables from external databases. To create such a query, perform the following steps:
To create a Query object by the ExternalDatabase::CreateQuery method. The method argument must be Null.
To set an SQL statement SELECT by the Query::SQL property in the query.
To set parameters of SQL statement SELECT by creating objects SQLParameter, setting their values and adding to the Query::SQLParameters collection.
To catalog a query, it is necessary to set its name using the Query::Name property and add the object to the GeoDatabase::Queries collection if the query is based on GDB tables or to the ExternalDatabase::Queries collection if the query is based on external database tables. It is impossible to catalog queries based on temporary tables.
If adding the query to the collection has succeeded, this query gets an internal number. Continuity of internal numbers of cataloged queries is not guaranteed. Assigned internal numbers do not change between GDB sessions for queries belonging to the GeoDatabase::Queries collection or to the ExternalDatabase::Queries collection (if an external database cataloged in GDB). Internal numbers may change at GDB reorganization performed by the GDBEngine::CompactGDB method with the gdbCompactNoKeepISN mode. Besides, internal numbers of cataloged queries from an external database not cataloged in GDB may change after cataloging the external database in GDB.
To access cataloged queries in the GeoDatabase::Queries or ExternalDatabase::Queries collection, use the Item method. The argument of the method can be a string (the query name) or an integer (the query internal number).
To access query fields in the Query::Fields collection, use the Item method. The argument of the method can be a string (the field name) or an integer (the field internal number). The collection provides continuity of field numbers.