This section concludes with a comprehensive example of using dynamic queries, buffers, temp-tables, and browses. The final example is saved as h-testDynTT.p.

To extend the current example to get some data into the temp-table and display it:
  1. Create a new procedure called h-testDynTT.p.
  2. Add a HANDLE variable for a dynamic query and some variables to display data in:
    /* Procedure h-testDynTT.p -- test dynamic temp-table
      methods. */
    DEFINE VARIABLE hTT AS HANDLE NO-UNDO.
    DEFINE VARIABLE hTTBuf AS HANDLE NO-UNDO.
    DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
    DEFINE VARIABLE iSeq AS INTEGER NO-UNDO INITIAL 1000.
    DEFINE VARIABLE cName LIKE Customer.NAME NO-UNDO.
    DEFINE VARIABLE iCNum LIKE Customer.CustNum NO-UNDO.
    DEFINE VARIABLE cRep LIKE SalesRep.RepName NO-UNDO.
  3. Add code to create the temp-table, add its fields and indexes, then call TEMP-TABLE-PREPARE():
    CREATE TEMP-TABLE hTT.
    hTT:CREATE-LIKE("Customer","Name").
    hTT:ADD-FIELDS-FROM("SalesRep","MonthQuota").
    hTT:ADD-LIKE-FIELD("Area", "SalesRep.Region").
    hTT:ADD-NEW-FIELD("Sequence", "INTEGER",0,"9999",1000).
    hTT:ADD-LIKE-INDEX("CustNum","CustNum","Customer").
    hTT:ADD-NEW-INDEX("SeqIndex", YES).
    hTT:ADD-INDEX-FIELD("SeqIndex", "Sequence").
    
    hTT:TEMP-TABLE-PREPARE("CustSequence").
  4. After the call to the TEMP-TABLE-PREPARE method, capture the DEFAULT-BUFFER-HANDLE in a variable:
    hTTBuf = hTT:DEFAULT-BUFFER-HANDLE.
  5. Define a static FOR EACH block to populate the temp-table with Customer and SalesRep values for Customers in New Hampshire, as well as a unique sequence value:
    /* Populate the temp-table with values from the database. */
    FOR EACH Customer WHERE State = "NH", SalesRep OF Customer:
      hTTBuf:BUFFER-CREATE().
      hTTBuf:BUFFER-COPY(BUFFER Customer:HANDLE).
      hTTBuf:BUFFER-COPY(BUFFER SalesRep:HANDLE).
      hTTBuf:BUFFER-FIELD("Sequence"):BUFFER-VALUE = iSeq.
      iSeq = iSeq + 1.
    END.
  6. Create a dynamic query for the temp-table buffer, prepare it to iterate through all the records in the temp-table, and open it:
    /* Now create a query for the temp-table buffer and display values. */
    CREATE QUERY hQuery.
    hQuery:SET-BUFFERS(hTTBuf).
    hQuery:QUERY-PREPARE("FOR EACH CustSequence").
    hQuery:QUERY-OPEN().
    hQuery:GET-FIRST().
  7. Walk through the query’s result list and copy some fields from the temp-table into the variables you defined. Display the values in a frame:
    REPEAT WHILE NOT hQuery:QUERY-OFF-END:
      ASSIGN iSeq = hTTBuf:BUFFER-FIELD("Sequence"):BUFFER-VALUE
        cName = hTTBuf:BUFFER-FIELD("Name"):BUFFER-VALUE
        iCNum = hTTBuf:BUFFER-FIELD("CustNum"):BUFFER-VALUE
        cRep = hTTBuf:BUFFER-FIELD("RepName"):BUFFER-VALUE.
      DISPLAY iSeq FORMAT "9999" LABEL "Sequence"
        iCNum
        cName FORMAT "X(20)"
        cRep FORMAT "X(20)" WITH FRAME CustSeqFrame 12 DOWN.
      hQuery:GET-NEXT().
    END.

    In a more general-purpose procedure, you could create dynamic fill-ins for each of the fields you want to display and capture their format, label, and other attributes from the buffer fields of the temp-table.

  8. Run the procedure:

    The primary index for the temp-table is the Sequence index, so the records come back in that order.