Try OpenEdge Now
skip to main content
ProDataSets
Advanced Events and Attributes : Successive loading of ProDataSet data : Using COPY-DATASET with a dynamic target ProDataSet
 

Using COPY-DATASET with a dynamic target ProDataSet

This first COPY-DATASET example shows how to copy a static source ProDataSet to a dynamic target, and have the AVM create the structure of the target before copying into it. The sample procedure is called DynCopy.p. It first includes the same static temp-table and ProDataSet definitions used elsewhere. It then defines variables to hold the handles of the target dynamic ProDataSet, its top-level buffer, and a dynamic query for that buffer, as shown:
/* DynCopy.p -- test procedure for COPY-DATASET to a dynamic Target. */
{dsOrderTT.i}
{dsOrder.i}

DEFINE VARIABLE hDataSet2 AS HANDLE NO-UNDO.
DEFINE VARIABLE hQuery2 AS HANDLE NO-UNDO.
DEFINE VARIABLE hBuffer2 AS HANDLE NO-UNDO.
The procedure next defines a static query for the Order table, along with the Data-Sources the ProDataSet uses, as shown:
DEFINE QUERY qOrder FOR Order.

DEFINE DATA-SOURCE srcOrder FOR QUERY qOrder.
DEFINE DATA-SOURCE srcOline FOR OrderLine.
DEFINE DATA-SOURCE srcItem  FOR ITEM.
It then prepares the source ProDataSet query to retrieve Orders for Customer 1, along with their OrderLines and Items, and attaches the Data-Sources. The FILL brings all this data into the source ProDataSet, as shown:
QUERY qOrder:QUERY-PREPARE("FOR EACH Order WHERE Order.custnum = 1").
BUFFER ttOrder:ATTACH-DATA-SOURCE(DATA-SOURCE srcOrder:HANDLE).
BUFFER ttOline:ATTACH-DATA-SOURCE(DATA-SOURCE srcOline:HANDLE).
BUFFER ttItem:ATTACH-DATA-SOURCE(DATA-SOURCE srcItem:HANDLE).
DATASET dsOrder:FILL().
A simple DISPLAY loop confirms that the Orders are in the source ProDataSet dsOrder and its ttOrder temp-table, as shown:
FOR EACH ttOrder:
  DISPLAY "Original Order: " ttOrder.OrderNum ttOrder.CustNum
    WITH FRAME Order1 20 DOWN.
END.
The procedure creates the dynamic ProDataSet using the handle hDataSet2, as shown:
CREATE DATASET hDataSet2.
hDataSet2:COPY-DATASET(DATASET dsOrder:HANDLE).
This is initially an empty structure for a ProDataSet. It has no table or relation definitions. The COPY-DATASET method copies the table and Data-Relation structure from dsOrder to the new dynamic ProDataSet, and then its data. To verify that both the definition and its data have been copied, the procedure creates a dynamic query for the top-level table in the new ProDataSet and prepares the query to navigate its rows.
As explained above, the COPY-DATASET method gives the new dynamic buffer the name "cpy_" plus the source buffer name, as shown:
CREATE QUERY hQuery2.
hQuery2:ADD-BUFFER(hDataSet2:GET-BUFFER-HANDLE(1)).
/* Note: the buffer name is cpy_ttOrder: */
hQuery2:QUERY-PREPARE("FOR EACH " + hDataSet2:GET-BUFFER-HANDLE(1):NAME).
The procedure opens the dynamic query and walks through all the rows at the top level of the target ProDataSet, displaying the same OrderNum and CustNum fields to verify that it contains the same data as the source, as shown:
hQuery2:QUERY-OPEN().
hQuery2:GET-FIRST().
hBuffer2 = hQuery2:GET-BUFFER-HANDLE.

DO WHILE NOT hQuery2:QUERY-OFF-END:
  DISPLAY "Copy of Order: "
    hBuffer2:BUFFER-FIELD("OrderNum"):BUFFER-VALUE COLUMN-LABEL "OrderNum"
    hBuffer2:BUFFER-FIELD("CustNum"):BUFFER-VALUE COLUMN-LABEL "CustNum"
    WITH FRAME Order2 20 DOWN.
  hQuery2:GET-NEXT().
  DOWN WITH FRAME Order2.
END.
When you run the procedure it shows you the Orders in both ProDataSets: