我从查询中填充了一个临时表,临时表看起来像是
ttcomp.inum
ttcomp.iname
ttcomp.iadd
此临时表中有5000条记录,现在我想写入CSV文件。我认为可以用output stream
完成,但我不知道如何实现它。请有人帮我解决这个问题。
答案 0 :(得分:9)
出口可以解决问题:
/* 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 */
答案 1 :(得分:1)
还有替代方法。 EXPORT很好,因为格式无关紧要,因为EXPORT忽略格式。例如,您不希望日期格式为mm / dd / yy。在这种情况下,最好使用PUT STREAM并明确指定格式。
FOR EACH ttComp NO-LOCK:
PUT STREAM str UNFORMATTED SUBSTITUTE("&1;&2;&3 \n", ttComp.inum, ttcomp.iname, ttcomp.iadd).
END.
答案 2 :(得分:0)
这是一种没有流的替代方案。
/* 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.