Progress
Language Reference


OPEN QUERY Statement

Interfaces
OS
SpeedScript
All
All
Yes

Opens a query, which might have been previously defined in a DEFINE QUERY statement. Opening a query makes it available for use within a GET statement, or in a browse widget.

SYNTAX

OPEN QUERY query { FOR | PRESELECT } EACH record-phrase
[ , { EACH | FIRST | LAST } record-phrase ] ...
[ query-tuning-phrase ]
[ BY expression [ DESCENDING ]
| COLLATE ( string , strength [ , collation ] ) [ DESCENDING ]
] ...
[ INDEXED-REPOSITION ]
[ MAX-ROWS num-results ] 

query

The query to open. The query name may have been defined previously in a DEFINE QUERY statement. Otherwise, the OPEN QUERY statement implicitly defines the query.

{ FOR | PRESELECT } EACH record-phrase

Specifies the first buffer of the query. The following is the syntax for record-phrase.

SYNTAX
record
[ [ LEFT ]]
  [ OF table ]
  [ WHERE expression ]
  [ USING [ FRAME frame ] field 
      [ AND [ FRAME frame ] field ] ...
  ]
  [ USE-INDEX index ]
  [ SHARE-LOCK | EXCLUSIVE-LOCK | NO-LOCK ]
  [ NO-PREFETCH ] 

If the query was previously defined, the buffers referenced by the record-phrase must be the same buffers referenced in the DEFINE QUERY statement and in the same order. For more information, see the Record Phrase reference entry.

Note that the first buffer must be qualified with EACH rather than the FIRST option. That is, the OPEN QUERY statement implies the possibility of a multi-row result, whether or not only one row is returned.

If you specify PRESELECT rather than FOR, then Progress preselects the records for the query. During the preselect process, Progress applies whatever locking is specified in the OPEN QUERY statement or, if none is specified, SHARE-LOCK. It then reads the ROWID for each record into the result list. (If you do not specify PRESELECT, Progress might pass through the records anyway to presort them. In this case, Progress applies NO-LOCK to each record during this pass.)

{ EACH | FIRST | LAST } record-phrase

Specifies subsequent buffers in the query. Each subsequent buffer specifies a join with the previous buffer(s) according to the record-phrase. If the query was previously defined, the buffers referenced by the record-phrase must be the same buffers referenced in the DEFINE QUERY statement and in the same order. For more information on specifying joins in Record phrases, see the Record Phrase reference entry.

query-tuning-phrase

Allows programmatic control over the execution of a DataServer query.

SYNTAX
QUERY-TUNING
 (
    [ LOOKAHEAD [ CACHE-SIZE integer ] | NO-LOOKAHEAD ]
    [ DEBUG { SQL | EXTENDED } | NO-DEBUG ]
    [ SEPARATE-CONNECTION | NO-SEPARATE-CONNECTION ]
    [ JOIN-BY-SQLDB | NO-JOIN-BY-SQLDB ]
    [ BIND-WHERE | NO-BIND-WHERE ]
    [ INDEX-HINT | NO-INDEX-HINT ]
  ) 

For more information, see your Progress DataServer Guides , Progress DataServer for ODBC Guide and Progress DataServer for ORACLE Guide.

BY expression [ DESCENDING ]

Specifies the order in which records are to be returned. If an index is defined with the right leading keys to satisfy the BY clause, Progress uses that index to sort the records. Otherwise, Progress must presort the records before the first fetch when you specify BY.

string

A CHARACTER expression that evaluates to the string whose collation value you want to compute.

strength

A CHARACTER expression that evaluates to one of the following:

collation

A CHARACTER expression that evaluates to the name of a collation table.

If collation does not appear, COLLATE uses the collation table of the client.

Progress reports an error and stops execution if one of the following occurs:

INDEXED-REPOSITION

If you specify this option, Progress attempts to optimize subsequent REPOSITION TO ROWID operations on the query. This can improve the performance of REPOSITION operations that must jump over many records in a simple query. Optimization is not possible if the database is not a Progress database, or sorting or preselection is performed. In these cases, the INDEXED-REPOSITION option is ignored and no error is reported.

The optimization has some side effects. When you perform a REPOSITION TO ROWID with this optimization, Progress discards the original result list and begins a new one. Therefore, scrolling forward or backward in the list might return different records from before. Also, the values of the NUM-RESULTS and CURRENT-RESULT-ROW become invalid. If the query has an associated browse, any selections in that browse are also lost. Lastly, the vertical scrollbar thumb is disabled. Because of these side-effects, use this option selectively.

MAX-ROWS num-results

Specifies the maximum number of records to be returned by the query. Any other records satisfying the query are ignored and no error is raised. The limit is imposed before any sorting occurs; Progress retrieves records up to the number specified and then sorts those records.

This option is valid for scrolling queries only. You can use it to prevent a long delay that might occur if a query returns many more records than you expect.

EXAMPLE

The following example opens a query on the customer, order, order-line, and item table.

r-opqury.p
DEFINE QUERY q-order FOR
    customer FIELDS (customer.cust-num customer.name customer.phone), 
    order FIELDS (order.order-num order.order-date),
    order-line FIELDS (order-line.line-num order-line.price 
                       order-line.qty),
    item FIELDS (item.item-num item.item-name item.cat-desc).

OPEN QUERY q-order FOR EACH customer,
    EACH order OF customer,
    EACH order-line OF order,
    EACH item OF order-line NO-LOCK.
 
GET FIRST q-order.

DO WHILE AVAILABLE(customer):

  DISPLAY customer.cust-num 
    customer.name skip
    customer.phone skip
    order.order-num order.order-date skip
    order-line.line-num
    order-line.price order-line.qty skip
    item.item-num item.item-name skip
    item.cat-desc VIEW-AS EDITOR SIZE 50 BY 2 SCROLLBAR-VERTICAL
    WITH FRAME ord-info CENTERED SIDE-LABELS TITLE "Order Information".

  /* Allow scrolling, but not modification, of cat-desc. */
  ASSIGN item.cat-desc:READ-ONLY IN FRAME ord-info = TRUE
         item.cat-desc:SENSITIVE IN FRAME ord-info = TRUE.
  
  PAUSE.
  
  GET NEXT q-order.
    
END. /* DO WHILE AVAIL(customer) */            

Note the use of field lists in the DEFINE QUERY statement. This can improve the performance of remote database queries significantly.

NOTES

SEE ALSO

CLOSE QUERY Statement, CREATE QUERY Statement, CURRENT-RESULT-ROW Function, DEFINE BROWSE Statement, DEFINE QUERY Statement, GET Statement, NUM-RESULTS Function, QUERY-OFF-END Function, QUERY-PREPARE( ) Method, REPOSITION Statement


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095