Steps for implementing Query-by-Example
This topic describes the steps involved to implement query-by-example using the
CONSTRUCT statement.
-
Define fields linked to database columns in a form specification file. The form field must be
defined with the same data type as SQL columns: Use a
SCHEMAclause,TABLESsection and use tabname.colname in theATTRIBUTESsection. - Define a
STRINGvariable in your program to hold the query criteria. - Open a window and display the form.
- Activate the form with the interactive dialog statement
CONSTRUCT, for entry of the query criteria.Control is turned over to the user to enter his criteria. - The user enters his criteria in the fields specified in the
CONSTRUCTstatement.TheCONSTRUCTstatement accepts logical operators in any of the fields to indicate ranges, comparisons, sets, and partial matches. Using the form in this program, for example, the user can enter a specific value, such as "IL" in the state field, to retrieve all the rows from the customer table where the state column = IL. Or he can enter relational tests, such as "> 103", in the customer number field, to retrieve only those rows where thecust_numcolumn is greater than 103. - After entering his criteria, the user selects OK, to instruct your
program to continue with the query, or Cancel to
terminate the dialog.In this program, the action views for accept (OK) and cancel are displayed as buttons on the screen.
-
If the user accepts the dialog, the
CONSTRUCTstatement creates an SQL expression by generating an SQL condition for each field with a criteria.This SQL expression is stored in the character string that you specified in theCONSTRUCTstatement. -
You can then use the SQL expression to create a
STRINGvariable containing a completeSELECTstatement.You must supply theWHEREkeyword followed by the SQL expression produced by theCONSTRUCT. - Execute the statement to retrieve the row(s) from the database table, after preparing it or
declaring a cursor for
SELECTstatements that might retrieve more than one row.