如何遍历使用缓冲区访问的 temp-table?
How to iterate through temp-table that is being accessed using buffer?
我找到了一个 article,关于从 class 外部访问在 class 内部声明的 temp-table。
我正在尝试使用第二种解决方案(使用句柄和缓冲区)。虽然它有效(我可以从中获取第一个或最后一个元素),但我找不到遍历完整临时文件的方法-table.
谢谢。
您可以通过创建动态查询并将其附加到缓冲区句柄来实现:
Class2.cls 看起来完全一样,如果在下面添加以供参考。
Test2.p 更改为:
DEF VAR c-class2 AS Class2.
DEF VAR local-ttbuf-hdl AS HANDLE.
DEF VAR tt-def-buff AS HANDLE.
c-class2 = NEW Class2().
local-ttbuf-hdl = c-class2:GetTempTableHandle().
tt-def-buff = local-ttbuf-hdl:DEFAULT-BUFFER-HANDLE.
/*
/* Find last is commented out */
tt-def-buff:FIND-LAST().
MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
tt-def-buff:BUFFER-FIELD(2):buffer-value
VIEW-AS ALERT-BOX.
*/
/**** New code here ****/
/* Define and create a dynamic query */
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE QUERY hQuery.
/* Set the buffer */
hQuery:SET-BUFFERS(tt-def-buff).
/* Create a query-string */
hQuery:QUERY-PREPARE("FOR EACH " + tt-def-buff:NAME).
/* Open the query */
hQuery:QUERY-OPEN.
REPEAT :
/* Get the next record, for the first run it will be the first record */
hQuery:GET-NEXT().
/* Leave the repeat if there are no more records */
IF hQuery:QUERY-OFF-END THEN LEAVE.
/* Display */
MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
tt-def-buff:BUFFER-FIELD(2):buffer-value
VIEW-AS ALERT-BOX.
END.
/* All created objects should be deleted */
DELETE OBJECT hQuery.
Class2.cls:
CLASS class2:
DEF VAR i AS INTEGER.
DEF TEMP-TABLE tt
FIELD i1 AS INTEGER
FIELD c1 AS CHARACTER.
CONSTRUCTOR class2():
DO i = 1 TO 10:
CREATE tt.
ASSIGN
tt.i1 = i
tt.c1 = STRING(i).
END.
END CONSTRUCTOR.
METHOD PUBLIC HANDLE GetTempTableHandle():
RETURN TEMP-TABLE tt:HANDLE.
END.
END CLASS.
我找到了一个 article,关于从 class 外部访问在 class 内部声明的 temp-table。 我正在尝试使用第二种解决方案(使用句柄和缓冲区)。虽然它有效(我可以从中获取第一个或最后一个元素),但我找不到遍历完整临时文件的方法-table.
谢谢。
您可以通过创建动态查询并将其附加到缓冲区句柄来实现:
Class2.cls 看起来完全一样,如果在下面添加以供参考。
Test2.p 更改为:
DEF VAR c-class2 AS Class2.
DEF VAR local-ttbuf-hdl AS HANDLE.
DEF VAR tt-def-buff AS HANDLE.
c-class2 = NEW Class2().
local-ttbuf-hdl = c-class2:GetTempTableHandle().
tt-def-buff = local-ttbuf-hdl:DEFAULT-BUFFER-HANDLE.
/*
/* Find last is commented out */
tt-def-buff:FIND-LAST().
MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
tt-def-buff:BUFFER-FIELD(2):buffer-value
VIEW-AS ALERT-BOX.
*/
/**** New code here ****/
/* Define and create a dynamic query */
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE QUERY hQuery.
/* Set the buffer */
hQuery:SET-BUFFERS(tt-def-buff).
/* Create a query-string */
hQuery:QUERY-PREPARE("FOR EACH " + tt-def-buff:NAME).
/* Open the query */
hQuery:QUERY-OPEN.
REPEAT :
/* Get the next record, for the first run it will be the first record */
hQuery:GET-NEXT().
/* Leave the repeat if there are no more records */
IF hQuery:QUERY-OFF-END THEN LEAVE.
/* Display */
MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
tt-def-buff:BUFFER-FIELD(2):buffer-value
VIEW-AS ALERT-BOX.
END.
/* All created objects should be deleted */
DELETE OBJECT hQuery.
Class2.cls:
CLASS class2:
DEF VAR i AS INTEGER.
DEF TEMP-TABLE tt
FIELD i1 AS INTEGER
FIELD c1 AS CHARACTER.
CONSTRUCTOR class2():
DO i = 1 TO 10:
CREATE tt.
ASSIGN
tt.i1 = i
tt.c1 = STRING(i).
END.
END CONSTRUCTOR.
METHOD PUBLIC HANDLE GetTempTableHandle():
RETURN TEMP-TABLE tt:HANDLE.
END.
END CLASS.