Writing Temp table value into .csv file

2019-09-16 16:40发布

问题:

We have created one temp-table dynamically . and we want to out put the complete table into a .csv file. how to do?

DEF VAR ttH AS HANDLE NO-UNDO.
ttH:CREATE-LIKE(hBuffer).
ttH:Temp-table-prepare("tmytable")

回答1:

Because you created your temp-table dynamically, you cannot use the EXPORT statement (at least to my knowledge). What you can do though is dynamically output first the fieldnames and then the field values of your table. Like so:

DEFINE VARIABLE hQuery  AS HANDLE NO-UNDO.
DEFINE VARIABLE ttH     AS HANDLE NO-UNDO.

/* Define a new output stream  */
DEFINE STREAM s1.
OUTPUT stream s1 to "myCSV.csv".

/* Create a query for the contents of your temp-table */
CREATE QUERY hQuery.
/* Important: we need to use the temp-tables default buffer handle, 
/* not the hBuffer handle to the original table! */
hQuery:SET-BUFFERS (ttH:DEFAULT-BUFFER-HANDLE).
/* Use the name you created before for the FOR EACH clause */
hQuery:QUERY-PREPARE("FOR EACH tmytable").
hQuery:QUERY-OPEN().
hQuery:GET-FIRST().

/* Write out the names of the fields as header in the first row */
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DO i = 1 TO ttH:DEFAULT-BUFFER-HANDLE:NUM-FIELDS:
    PUT STREAM s1 UNFORMATTED ttH:DEFAULT-BUFFER-HANDLE:buffer-field(i):name + ",".
END.
PUT STREAM s1 UNFORMATTED SKIP.

/* Walk through the query and output each field seperately. */
REPEAT:  
    hQuery:GET-NEXT().  
    IF hQuery:QUERY-OFF-END THEN LEAVE.  

    DO i = 1 TO ttH:DEFAULT-BUFFER-HANDLE:NUM-FIELDS:
        PUT STREAM s1 UNFORMATTED ttH:DEFAULT-BUFFER-HANDLE:buffer-field(i):buffer-value ",".
    END.
    PUT STREAM s1 UNFORMATTED SKIP.
END.

OUTPUT stream s1 close.

Just note that this might not perform for large quantities of table entries!



回答2:

See Progress KB P4410 for extent handling syntax. You might also want to add FORWARD-ONLY=TRUE to the query before QUERY-PREPARE to possibly boost performance.