如何创建一个正在进行动态字段编号的临时表(opensge 4gl)

时间:2014-04-11 09:32:18

标签: progress-4gl openedge

如何使用它来定义临时表...

CREATE TEMP-TABLE tt.

1 个答案:

答案 0 :(得分:2)

直接从文档中解除:

DEFINE VARIABLE tth          AS HANDLE NO-UNDO.
DEFINE VARIABLE bh           AS HANDLE NO-UNDO.
DEFINE VARIABLE qh           AS HANDLE NO-UNDO.
DEFINE VARIABLE buf-ord-hndl AS HANDLE NO-UNDO.
DEFINE VARIABLE buf-rep-hndl AS HANDLE NO-UNDO.
DEFINE VARIABLE fld1         AS HANDLE NO-UNDO.
DEFINE VARIABLE fld2         AS HANDLE NO-UNDO.

/* Get database table handles */
buf-ord-hndl = BUFFER Order:HANDLE.
buf-rep-hndl = BUFFER SalesRep:HANDLE.

/* Create an empty, undefined TEMP-TABLE */
CREATE TEMP-TABLE tth.
/* Give it Order table’s fields & indexes */
tth:CREATE-LIKE(buf-ord-hndl).
/* Add field like SalesRep.RepName */
tth:ADD-LIKE-FIELD("RepName","SalesRep.RepName").
/* No more fields will be added */
tth:TEMP-TABLE-PREPARE("ordx").

/* Get the buffer handle for the temp-table */
bh = tth:DEFAULT-BUFFER-HANDLE.

/* Populate the temp-table from order */
FOR EACH Order NO-LOCK:
  bh:BUFFER-CREATE.
  bh:BUFFER-COPY(buf-ord-hndl).
  /* Add the corresponding salesrep name */
  FIND SalesRep NO-LOCK WHERE SalesRep.SalesRep = Order.SalesRep NO-ERROR.
  IF AVAILABLE SalesRep THEN
    bh:BUFFER-COPY(buf-rep-hndl,?,"RepName,repname").
END. 

/* Run a query to access the TEMP-TABLE */
CREATE QUERY qh.
qh:SET-BUFFERS(bh).
qh:QUERY-PREPARE("FOR EACH ordx WHERE ordx.OrderNum < 50 BY ordx.RepName").
qh:QUERY-OPEN().

fld1 = bh:BUFFER-FIELD("OrderNum").
fld2 = bh:BUFFER-FIELD("RepName").

/* Display the order number and the salesrep name */
REPEAT:
  qh:GET-NEXT().
  IF qh:QUERY-OFF-END THEN LEAVE.
  DISPLAY fld1:BUFFER-VALUE() FORMAT "X(10)".
  DISPLAY fld2:BUFFER-VALUE() FORMAT "X(20)".
END.

qh:QUERY-CLOSE().
bh:BUFFER-RELEASE().
DELETE OBJECT tth.
DELETE OBJECT qh.

如果你是从头开始构建,你可能想要使用ADD-NEW-FIELD()而不是ADD-LIKE-FIELD()。

如果您有XML架构,您还可以使用它来创建TT,例如:

DEFINE VARIABLE lRetOK                  AS LOGICAL   NO-UNDO.
DEFINE VARIABLE cSourceType             AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFile                   AS CHARACTER NO-UNDO.
DEFINE VARIABLE lOverrideDefaultMapping AS LOGICAL   NO-UNDO.
DEFINE VARIABLE cFieldTypeMapping       AS CHARACTER NO-UNDO.
DEFINE VARIABLE cVerifySchemaMode       AS CHARACTER NO-UNDO.
DEFINE VARIABLE hTable                  AS HANDLE    NO-UNDO.

CREATE TEMP-TABLE hTable.

ASSIGN
  cSourceType             = "file"
  cFile                   = "ttcust.xsd"
  lOverrideDefaultMapping = FALSE
  cFieldTypeMapping       = "address2,CLOB"
  cVerifySchemaMode       = ?.

lRetOK = hTable:READ-XMLSCHEMA (cSourceType, cFile, lOverrideDefaultMapping,
  cFieldTypeMapping,cVerifySchemaMode).