将Temp表值写入.csv文件 [英] Writing Temp table value into .csv file

查看:172
本文介绍了将Temp表值写入.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们动态创建了一个临时表.并且我们想将整个表放入一个.csv文件中.怎么办?

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")

推荐答案

由于动态创建了临时表,因此您不能使用EXPORT语句(至少据我所知).但是,您可以做的是先动态输出表的字段名,然后再动态输出表的字段值.像这样:

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!

这篇关于将Temp表值写入.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆