openedgeprogress-4glprogress-db

Dynamic Sort BY in a Query Prepare with a Join


I want to join pt_mstr and in_mstr to create my report screen for which I have written this code i want to sort my output screen based on either prod line or status so i have defined a variable lvc_sort sort by prod line if i give 1 and similarly 2 for status

IF lvc_sort = 1 THEN DO:

         FOR EACH pt_mstr no-lock
         WHERE pt_domain       =  global_domain
         AND   pt_part        >=  lvc_part
         AND   pt_part        <=  lvc_part1
         AND   pt_part_type   >=  lvc_part_type
         AND   pt_part_type   <=  lvc_part_type1
         AND   pt_prod_line   >=  lvc_prod_line
         AND   pt_prod_line   <=  lvc_prod_line1
         AND   pt_status      >=  lvc_status
         AND   pt_status      <=  lvc_status1,

         EACH in_mstr
         WHERE in_domain  =  pt_domain
         AND   in_part =  pt_part
         BREAK BY pt_prod_line:

         FIND FIRST tt NO-LOCK
         WHERE tt_part  = pt_part
         AND   tt_site = in_site NO-ERROR.

Is this the correct approach or can it be done in some other way?


Solution

  • You can use a query then you have a section with a different by clause

    DEFINE VARIABLE cQuery AS CHARACTER NO-UNDO.
    DEFINE VARIABLE hQuery AS HANDLE    NO-UNDO.
    
    cQuery = "FOR EACH pt_mstr no-lock " +
             " WHERE pt_domain       =  " + QUOTER(global_domain) +
             " AND   pt_part        >=  " + QUOTER(lvc_part) +
             " AND   pt_part        <=  " + QUOTER(lvc_part1) +
             " AND   pt_part_type   >=  " + QUOTER(lvc_part_type) +
             " AND   pt_part_type   <=  " + QUOTER(lvc_part_type1) +
             " AND   pt_prod_line   >=  " + QUOTER(lvc_prod_line) +
             " AND   pt_prod_line   <=  " + QUOTER(lvc_prod_line1) +
             " AND   pt_status      >=  " + QUOTER(lvc_status) +
             " AND   pt_status      <=  " + QUOTER(lvc_status1) +
    
             ", EACH in_mstr " +
             " WHERE in_domain  =  " + QUOTER(pt_domain) +
             " AND   in_part =  " + QUOTER(pt_part).
    
    CREATE QUERY hQuery. 
    hQuery:SET-BUFFERS(BUFFER pt_mstr:HANDLE, BUFFER in_mstr:HANDLE).
    
    IF lvc_sort = 1 THEN
        cQuery = cQuery + " BREAK BY pt_prod_line ".
    ELSE
        cQuery = cQuery + " BREAK BY pt_status ". 
    
    hQuery:QUERY-PREPARE(cQuery).
    hQuery:QUERY-OPEN.
    
    // alle durch
    DO WHILE hQuery:GET-NEXT() :
       
       FIND FIRST tt NO-LOCK
         WHERE tt_part  = pt_part
         AND   tt_site = in_site NO-ERROR.
       
    END.
    
    
    FINALLY:
       hQuery:QUERY-CLOSE () NO-ERROR.
       DELETE OBJECT hQuery NO-ERROR.
    END FINALLY.