Progress
Language Tutorial
for Character


Querying and Sorting Example

Follow these steps for a demonstration of combining sorting options with selection options:

  1. Open lt-09-03.p and run it. The following interface appears:
  2. Choose the BY ASCENDING Price button. A browse widget appears with the results of the query. In the status area, notice the message that shows the selection and sorting syntax used in the OPEN QUERY statement.
  3. Choose the next two buttons.
  4. Choose Exit, then press SPACEBAR to return to the Procedure Editor.

Here is the code that created the display:

lt-09-03.p
    /**********  DEFINE QUERIES  **********/
    DEFINE QUERY New-Query FOR Item FIELDS (Item-Num Item-Name Price).

    /**********  DEFINE WIDGETS  **********/
    DEFINE BROWSE New-Browse QUERY New-Query
        DISPLAY Item-Num Item-Name Price WITH 22 DOWN.
    DEFINE BUTTON btn-Q1 LABEL "BY ASCENDING Price ".
    DEFINE BUTTON btn-Q2 LABEL "BY DESCENDING Price".
    DEFINE BUTTON btn-Q3 LABEL "USE-INDEX Item-Num ".
    DEFINE BUTTON btn-Exit LABEL "Exit".

    /**********  DEFINE FRAMES  **********/
    DEFINE FRAME Frame1 
      SKIP(1) btn-Q1 COLON 50 SPACE(2) SKIP(1) btn-Q2 SKIP(1) 
      btn-Q3 SKIP(2) btn-Exit
      New-Browse AT ROW 2 COLUMN 2 
          WITH TITLE "Query Results" AT ROW 2 COLUMN 2 THREE-D.

    /**********  DEFINE TRIGGERS  **********/
    ON CHOOSE OF btn-Q1
    DO:
/*1*/      OPEN QUERY New-Query FOR EACH Item WHERE Item-Name BEGINS "S" 
              BY Price.
          DISPLAY New-Browse WITH FRAME Frame1.
          MESSAGE "FOR EACH Item WHERE Item-Name BEGINS ""S"" BY Price".
    END.
    ON CHOOSE OF btn-Q2
    DO:
/*2*/      OPEN QUERY New-Query FOR EACH Item WHERE Item-Name BEGINS "S"
              BY Price DESCENDING.
          DISPLAY New-Browse WITH FRAME Frame1.
           MESSAGE 
       "FOR EACH Item WHERE Item-Name BEGINS ""S"" BY Price DESCENDING".
    END.
    ON CHOOSE OF btn-Q3
    DO:
/*3*/     OPEN QUERY New-Query FOR EACH Item WHERE Item-Name BEGINS "S"
            USE-INDEX Item-Num.
          DISPLAY New-Browse WITH FRAME Frame1.
           MESSAGE 
       "FOR EACH Item WHERE Item-Name BEGINS ""S"" USE-INDEX Item-Num". 
    END. 
    /**********  MAIN LOGIC  **********/
    ENABLE ALL WITH FRAME Frame1.
    WAIT-FOR CHOOSE OF btn-Exit. 

These notes describe querying and sorting to help explain the code:

  1. The BY phrase sorts the selected records by a non-indexed field, Price. The sort is ascending by default.
  2. This BY phrase also sorts the selected records by Price, but the DESCENDING option forces a descending sort.
  3. The USE-INDEX option makes clear to the query which of the defined indexes to use for the sort order. Again, this sort is ascending by default.
  4. Practice Problems

    Problem 9-1:

    The code for the first example, lt-09-01.p, provides a nice shell for creating your own queries. Make a copy of the procedure and use it to experiment with querying. Use the different selection and sorting techniques. Also try using a different database table.


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