Can I build and execute dynamic SQL in SELCOPY?


Yes. Build your SQL in the work area, and use "SQL=nnn AT ppp" to execute it. The example below shows how different SQL statements can be built and executed. The explicit OPEN and CLOSE are required to allow the 'file' SAMPSQL to be read multiple times.


  =BLD=
     IF no more SQL to build
       THEN EOJ
     ...
     ... Build SQL at pos ppp
     ...
     OPEN SAMPSQL SQL=nnn at ppp
     =RDT=
      READ SAMPSQL
      IF EOF SAMPSQL
        THEN CLOSE SAMPSQL
            THEN GOTO  BLD
     ...
     ... Process data
     ...
     GOTO RDT