Query (QQQQRY) API


  Required Parameter Group:


  Default Public Authority: *EXCLUDE

  Threadsafe: Conditional; see Usage Notes.

The Query (QQQQRY) API gets a set of database records that satisfies a database query request. Using this API you can do all the things you could do with the Open Query File (OPNQRYF) command. You can also perform subqueries, perform unions, and use SQL host variables.

The QQQQRY API can be used to do any combination of the following database functions:

You can use this API to run a query, create an access plan, or get information from the query definition template (QDT). When you run the query, the API uses the information you provide with the query definition template to extract information and data from the database. Creating an access plan makes it possible to run the query with better performance. Checking the query definition template allows you to validate the values in this query definition template.

The format definition is part of the query definition template and can be created and saved with extracted information by the Retrieve Database File Description (QDBRTVFD) API. When you are finished using the QQQQRY API, you should close the file (using the Close File (CLOF) command) to free up resources.

Another part of the query definition template is the access plan for the query. Using this API with the Create Query Access Plan (CRTQAP) value of the query option requested parameter, you can build an access plan to more efficiently run a query more than once. You can then use the access plan control block parameter to point to the access plan. This greatly improves the time it takes to perform subsequent runs of this query using this API and the RUNQRY option. Every time a query is run, the system first checks to see if an access plan has been specified. If one has, that is what is used to get the data requested by the query. If no access plan has been specified, a new one is built dynamically.


Authorities and Locks

User Space Authority
*CHANGE
Library Authority
*EXECUTE
File Authority
*OBJOPR
User Space Lock
*SHRRD

Required Parameter Group

Query option requested
INPUT; CHAR(10)

One of three options to be used:

  • RUNQRY
  • Run query
  • CRTQAP
  • Create query access plan
  • CHKQDT
  • Check query definition templates

User file control block
I/O; CHAR(*)

One or more selected options for input and output of the specified query. This parameter need only be used along with the RUNQRY query option. See User File Control Block (QDBUFCB_T) Structure for a list of available options.

Query definition template
I/O; CHAR(*)

The information required to create objects that are used to query a database. It contains feedback information from the creation of objects. If a pointer to the access plan is specified, the corresponding query definition templates must also be specified.

Literal values
I/O; CHAR(*)

This parameter is used to put into effect SQL host variables. When SQL host variables are used, this is a list of constant values used to run a query. If this parameter is to be ignored, a null pointer can be specified for the parameter. Once the literal value is specified on a call, it must always be specified.

Access plan control block
I/O; CHAR(48)

A string of bytes that point to the access plan control block and give the size the access plan requires. This parameter must be specified for the RUNQRY query option when you want to specify an access plan and for the CRTQAP query option. The format for this parameter is:


Error code
I/O; CHAR(*)

The structure in which to return error information. For the format of the structure, see Error code parameter.


Data Structures

The QQQQRY API uses information in four structures to carry out a query. All structures are used together to perform the function you have selected using the query option requested parameter. The names of these structures are:


The following sections show you in a general way how this information is structured.


Query Definition Template (QDBQH_T)

The query definition template provides information about the query that is to be performed. QDBQH_T Format shows the general layout of this format.

Notice the box marked with a (1) in QDBQH_T Format. The topic Format Definition Template (Qdb_Qddfmt_t) provides the layout of the entire record format specification.

The offsets and descriptions of all the fields contained in this structure are shown in the following tables. You can see this source in member QQQQRY in the QSYSINC library.

QDBQH_T Format

QDBQH_T Format


Query Definition Header (QDBQH_T)

This is the first structure and is located at offset zero. (Ref #1.)



Sequence, Tables, Names, and Parameters (QQQNLSS_T)

Sequence, tables, names, and parameters structure. The displacement to this structure from the beginning of structure QDBQH_T is an entry in the table at variable qdbqnlss.



File Name Specification (QDBQF_T)

File name specification. This structure defines the files, member, and formats that are used in the query. This structure is required.



File, Library, Member, and Format Array (QDBQN_T)

File, library, member and format names array. This structure defines the file, library, member, and format names that are used in the query. This structure is required.



Record Format Specification (QDBQR_T)

Record format specification. This structure defines the fields that are used in the query. The structure Qdb_Qddfmt_t is mapped by member QDBRTVFD in the QSYSINC library. If join is specified, this specification is required.



Join Specification (QDBQJ_T)

Join specification. This structure defines how the files are joined by the query. One join specification exists for the entire query definition. A join specification entry consists of a from-field, a join operator, and a to-field. The join specification entries can be inserted in any order with respect to the file specifications.

If this is an inner join (the qdbqmfop field equals J, and no join specifications are given for a particular to-file, the system searches the record selection specifications for any possible implied join specifications. If no join specifications can be derived from the record selection specifications, Cartesian product is used to do the join.

All join specifications can be given in the record selection specifications. In this case, it is not necessary to provide a join specification.

If this is a partial-outer or exception join (qdbqmfop equals C or E) and no join specifications are given for a particular to-file, the system uses Cartesian product to do the join. In addition, only one join operator can be specified for a particular to-file.



Join Specification Array (QDBQJFLD_T)

Join specification array. This structure is an array of fields that define the from and to fields to use when joining.



JREF Join Specification (QDBQ_JREF_T)

JREF Join specification. This structure can be used to define the order in which the files are to be joined. It can also be used to specify any join selection needed to implement the join. Two files (or join results) are specified along with the appropriate join type to be used to join together the two operands. An offset can also be specified to the Selection Specifications (QDBQS) that will define the join criteria that applies to the operands.



JREF Join Entry (QDBQ_JREF_ENTRY_T)

JREF Join Entry.



JREF Join Operand (QDBQ_JREF_OPERAND_T)

JREF Join entry operand.



JREF Join Operator (QDBQ_JREF_OPERATOR_T)

JREF Join entry operator.



Record Selection Specification (QDBQS_T)

Record selection specification. This structure defines the selection specifications for the files being queried. Selection on the file is done before grouping. If selection is desired on group by results, see structure QDBQGS_T on Group-by-Selection Specification (QDBQGS_T).



Selection Item Specifications (QDBQSIT_T)

Selection item specifications. This structure is defined at field qdbqspec in structure QDBQS_T.



Selection Field Operand (QDBQSOPF_T)

Selection field operand. This structure overlays field qdbqsitm in structure QDBQSIT_T.



Selection Field Subquery Operand (QDBQSOPS_T)

Selection field subquery operand. This structure overlays field qdbqsitm in structure QDBQSIT_T).



Selection Constant Operand (QDBQSOPC_T)

Selection Constant Operand. This structure overlays field qdbqsitm in structure QDBQSIT_T.



Selection Operator Item (QDBQSOPR_T)

Selection Operator Item. This structure overlays field qdbqsitm in structure QDBQSIT_T.


Note: For the wildcard scan operator (qdbqsop=X'0042'), UTF-8 wildcard values should not be specified. If either the match operand, the pattern operand, or the escape character are UTF-8, specify both the EBCDIC equivalents (qdbqswc1, qdbqswc2, qdbqsdb1, and qdbqsdb2) and the UCS-2 equivalents (qdbqsuo1, qdbqsuo2, qdbqsum1, and qdbqsum2).



Selection Operator Item Extension (QDBQSOP3_T)

Selection Operator Item Extension. This structure overlays field qdbqsitm in structure QDBQSIT_T by following QDBQSOPR_T and is only present if qdbqsopr_ext is set to '1'.



Order by Specification (QDBQK_T)

Order by specification. This structure contains a description of how the results of the query should be ordered. Up to 10 000 bytes may be used in ordering.



Group by Specification (QDBQG_T)

Group by specification. This structure contains a description of how the record results of the query should be grouped. All records for which equal values exist in the defined fields are grouped together. Up to 2000 bytes may be used.



Group-by-Selection Specification (QDBQGS_T)

Group-by-selection specification. This structure defines the selection specifications for the group by results. Selection on the group results is performed after the selection on the record is complete and the grouping has been completed.



Set Operation Specification (QDBQT_T)

Set operation specification. This structure defines the operation specifications being performed for each set of results generated from each query definition template. These specifications are only valid when more than one query definition template is specified. The set operation specifications must only be specified on the last query definition template.

The specification structure is a stack of operands and operators in reverse notation. Operands are constant literals that identify the relative position of a query definition template among others in the query-definition-template chain. Operators are set operators such as union. For example, given the following query definition templates:

Query definition templates

The following operations can be performed:

         (1st QDT) UNION (2nd QDT) UNION ALL (3rd QDT)


The above can be specified in the set operation specification (in reverse notation) as:

         1 2 UNION 3 UNION ALL



Set Item Specifications (QDBQTIT_T)

Set item specifications. This structure overlays field qdbqtspc in structure QDBQT_T.



Relative Number of Query Definition Template (QDBQtopC_T)

Relative number of query definition template. This structure overlays field qdbqtitm in structure QDBQTIT_T .



Set Operators (QDBQtopR_T)

Set operators. This structure overlays field qdbqtitm in structure QDBQTIT_T .



Query Definition Template Offset Table (QDBQQDTS_T)

Query definition template offset table. This structure is set for each unioned outermost query definition template that contains subqueries. This offset table contains offsets for addressability to each query definition template within a union.



Array of Subquery Offsets (QDBQQDT_T)

Array of subquery offsets.



Format Definition Template (Qdb_Qddfmt_t)

The format definition (Qdb_Qddfmt_t) for the QQQQRY API is the same structure that is used by the Retrieve Database File Description (QDBRTVFD) API called FILD0200. Qdb_Qddfmt_t Format shows how this information is organized. When more than one entry can appear, the figure indicates this as in (2). For a description of the fields in Qdb_Qddfmt_t and their respective offsets, see FILD0200 Format (Qdb_Qddfmt Structure) in Retrieve Database File Description (QDBRTVFD) API.

The description and offsets are also in the include source supplied with IBM i. You can see this source in member QDBRTVFD in the QSYSINC library.

The QQQQRY API builds the format definition if it was not created prior to the query.

Qdb_Qddfmt_t Format

Qdb_Qddfmt_t Format


User File Control Block (QDBUFCB_T) Structure

User file control block. This structure holds information from the user file control block (UFCB). It contains selected options for the input and output of the specified query.

The options available include:

In addition, some validity checking is done for this UFCB. CPF4297 is issued if any reserved space in the header of the QDBUFCB_T format is not zero.

The offsets and a description of all the fields contained in this structure are shown in the following table. You can see this source in member QQQQRY in the QSYSINC library.



Value for Query Variable Fields (QQQVALS_T) Structure

The structure is used to supply the values for the variable fields used by the QQQQRY API. The offsets and a description of all the fields contained in this structure are shown in the following table. You can see this source in member QQQQRY in the QSYSINC library.



Usage Notes

In multithreaded jobs, this command is not threadsafe for distributed files and fails for distributed files that use relational databases of type *SNA. This command also is not threadsafe and fails for Distributred Data Management (DDM) files of type *SNA.


Error Messages



Example

For examples that use the QQQQRY API, see Example: Defining queries in the Examples: APIs and exit programs topic.



API introduced: V2R2

[ Back to top | Database and File APIs | APIs by category ]