3

I populated a temp from a query, and the temp table looks like,

ttcomp.inum
ttcomp.iname
ttcomp.iadd

There are 5000 records in this temp table and now i wanted to write in a CSV file. I think it could be done with output stream but i don't know how to implement this. Please someone help me in getting this.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
user3427690
  • 81
  • 1
  • 2
  • 5

3 Answers3

9

Export does the trick:

/* Define a stream */
DEFINE STREAM str.

/* Define the temp-table. I did some guessing according datatypes... */
DEFINE TEMP-TABLE ttcomp
    FIELD inum  AS INTEGER
    FIELD iname AS CHARACTER
    FIELD iadd  AS INTEGER.

/* Fake logic that populates your temp-table is here */
DEFINE VARIABLE i AS INTEGER     NO-UNDO.
DO i = 1 TO 5000:
    CREATE ttComp.
    ASSIGN 
        ttComp.inum  = i
        ttComp.iname = "ABC123"
        ttComp.iadd  = 3.

END.
/* Fake logic done... */

/* Output the temp-table */
OUTPUT STREAM str TO VALUE("c:\temp\file.csv").
FOR EACH ttComp NO-LOCK:
    /* Delimiter can be set to anything you like, comma, semi-colon etc */
    EXPORT STREAM str DELIMITER "," ttComp.
END.
OUTPUT STREAM str CLOSE.
/* Done */
Jensd
  • 7,886
  • 2
  • 28
  • 37
  • 1
    the OP asked for comma, not semi-colon delimited output. Otherwise the example's good. – Tim Kuehn May 14 '14 at 13:13
  • 1
    @TimKuehn good point. Just did as I usually do myself. I'll edit the answer. – Jensd May 14 '14 at 13:32
  • 1
    @Jensd cool, i prefer making my delimiter a variable with initial value set, so it is one place right at top of code to change the delimiter should someone wish to change from say "," to "|" – AquaAlex May 19 '14 at 09:05
  • @AquaAlex Thats a good way to do it! I just provided you with a basic example! :) – Jensd May 19 '14 at 09:07
1

Here is an alternative without stream.

/* Using the temp-table. of Jensd*/
DEFINE TEMP-TABLE ttcomp
    FIELD inum  AS INTEGER
    FIELD iname AS CHARACTER
    FIELD iadd  AS INTEGER.

OUTPUT TO somefile.csv APPEND.

    FOR EACH ttcomp:
                DISPLAY 
                    ttcomp.inum + ",":U  
                    ttcomp.iname + ",":U
                    ttcomp..iadd  SKIP. 
    END.

OUTPUT CLOSE.
1

There is alternative way also. EXPORT is good in case format doesn't matter because EXPORT ignores format. For example you don't want format for date to be mm/dd/yy. In this case it is better to use PUT STREAM and specify format explicitly.

FOR EACH ttComp NO-LOCK:
    PUT STREAM str UNFORMATTED SUBSTITUTE("&1;&2;&3 \n", ttComp.inum, ttcomp.iname, ttcomp.iadd).
END.
PovilasZ
  • 191
  • 1
  • 1
  • 15