Bharat
Bharat

Reputation: 177

How to get labels from temp table if its field datatype is LIKE table-name - Progress 4GL?

I am new to progress 4GL. I always use below query to export data from temp table as .csv file. To give header labels I have hard coded. Is it possible to get the labels from temp table fields itself? If yes pls help me by modifying the query.

DEFINE TEMP-TABLE ttdata NO-UNDO
FIELD cCustomerName LIKE Customer.NAME
FIELD cAddress      LIKE Customer.Address
.

OUTPUT TO VALUE(SESSION:TEMP-DIRECTORY + "temp.csv").

 PUT UNFORMATTED "customer Name,Customer Address" SKIP.

 FOR EACH ttdata NO-LOCK:
    EXPORT DELIMITER "," ttdata.
 END.

OUTPUT CLOSE.

Upvotes: 1

Views: 707

Answers (2)

Stefan Drissen
Stefan Drissen

Reputation: 3379

A character variable for the header reduces the amount of noise:

var int ifields,ic,icx.
var handle hb,hf.
var char cheader.

define temp-table ttdata no-undo
    field customername like customer.name
    field address      like customer.address
    field monthquota   like salesrep.monthquota    
    .

output to value( session:temp-directory + 'temp.csv' ).

hb = buffer ttdata:handle.
ifields = hb:num-fields.

do ic = 1 to ifields:

    hf = hb:buffer-field( ic ).

    do icx = if hf:extent > 0 then 1 else 0 to hf:extent:
    
        cheader = cheader + ',' + hf:name.
        if icx > 0 then 
            cheader = cheader + '[' + string( icx ) + ']'.
        
    end.

end.

put unformatted substring( cheader, 2 ) skip.

for each ttdata no-lock:
   export delimiter ',' ttdata.
end.

output close.

https://abldojo.services.progress.com/?shareId=624995c73fb02369b25437c5

Upvotes: 1

Mike Fechner
Mike Fechner

Reputation: 7192

You can iterate the buffer fields of the temp-table's default buffer like this:

DEFINE TEMP-TABLE ttdata NO-UNDO
    FIELD cCustomerName LIKE Customer.NAME
    FIELD cAddress      LIKE Customer.Address 
    FIELD cMonthQuota   LIKE Salesrep.MonthQuota    
    .

DEFINE VARIABLE i AS INTEGER NO-UNDO. 
DEFINE VARIABLE j AS INTEGER NO-UNDO. 
DEFINE VARIABLE iCount AS INTEGER NO-UNDO.         
DEFINE VARIABLE iExtent AS INTEGER     NO-UNDO.
          
OUTPUT TO VALUE(SESSION:TEMP-DIRECTORY + "temp.csv").

ASSIGN iCount = BUFFER ttData:NUM-FIELDS .

DO i = 1 TO iCount:
    iExtent = BUFFER ttData:BUFFER-FIELD (i):EXTENT .
    
    IF iExtent > 1 THEN
    DO:
        DO j = 1 TO iExtent:
            PUT UNFORMATTED 
                (IF i > 1 OR j > 1 THEN "," ELSE "") 
                SUBSTITUTE ("&1[&2]",
                            BUFFER ttData:BUFFER-FIELD (i):LABEL,
                            j) .
        END.        
    END.
    ELSE
        PUT UNFORMATTED 
            (IF i > 1 THEN "," ELSE "") 
            BUFFER ttData:BUFFER-FIELD (i):LABEL .
END.

PUT UNFORMATTED SKIP.

FOR EACH ttdata NO-LOCK:
   EXPORT DELIMITER "," ttdata.
END.

OUTPUT CLOSE.

Upvotes: 5

Related Questions