How to assign data from temp-table to dynamic temp-table

moush

New Member
I have been trying to solve this for a couble of days.
Can anyone help me please?
I create a dynamic temp-table with fields according to the user's selection from a selection-list. The name of these fields are assigned to the same value as the fields of an other temp-table (not dynamic). I need to match the fields in the dynamic temp-table with the ones in the temp-table and assign the value of the temp-table field to the dynamic temp-table field.

The code goes like this:

DEFINE VARIABLE hTable AS HANDLE NO-UNDO.
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE hBufferField AS HANDLE NO-UNDO.
DEFINE VARIABLE hBrowse AS HANDLE NO-UNDO.
DEFINE VARIABLE hCol AS HANDLE NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE j AS INTEGER NO-UNDO.

CREATE WIDGET-POOL.

/************ Create a dynamic temp-table object **********/
CREATE TEMP-TABLE hTable.

hTable:ADD-NEW-FIELD( "sino", "char", ?, "x(10)", ?, "CREWID", "CREWID").
hTable:ADD-NEW-FIELD( "lname", "char", ?, "x(20)", ?, "LAST NAME", "LAST NAME").
hTable:ADD-NEW-FIELD( "fname", "char", ?, "x(20)", ?, "FIRST NAME", "FIRST NAME").
hTable:ADD-NEW-FIELD( "nation", "char", ?, "x(5)", ?, "NATION", "NATION").
hTable:ADD-NEW-FIELD( "rank", "char", ?, "x(10)", ?, "RANK", "RANK").

DO cnt = 1 TO NUM-ENTRIES(report-items) WITH FRAME {&FRAME-NAME}:
itm[cnt] = ENTRY(cnt,report-items).
hTable:ADD-NEW-FIELD( ENTRY(cnt,report-items), "char", ?, "x(10)", ?, ENTRY(cnt,report-items), ENTRY(cnt,report-items)).
END.
hTable:TEMP-TABLE-PREPARE('report').
/************** Create the dynamic query object ************/
CREATE QUERY hQuery.

CREATE BUFFER hBuffer FOR TABLE hTable:DEFAULT-BUFFER-HANDLE.

hQuery:SET-BUFFERS(hBuffer).
hQuery:QUERY-PREPARE('FOR EACH report no-lock').

/** THE PROBLEM IS DOING THIS **/

FOR EACH mytable,
FIRST srch-table WHERE srch-table.sino = mytable.mtsino:
hBuffer:BUFFER-CREATE().
DO i = 1 TO hBuffer:NUM-FIELDS:
HERE I NEED TO ASSIGN THE VALUES OF MyTable TO THE CORRECT FIELD OF hBuffer .
END.
end.

Then I need to open the data of the hBuffer

Any ideas?
 
moush said:
I have been trying to solve this for a couble of days.
Can anyone help me please?
I create a dynamic temp-table with fields according to the user's selection from a selection-list. The name of these fields are assigned to the same value as the fields of an other temp-table (not dynamic). I need to match the fields in the dynamic temp-table with the ones in the temp-table and assign the value of the temp-table field to the dynamic temp-table field.

Try BUFFER-COPY - something like hbuffer:BUFFER-COPY(httable).

As an example. the following creates a dynamic temp-table from a static temp-table mytable, then copies the fields, then queries the dynamic temp-table and displays the 3 fields in a single display statement.

DEFINE VARIABLE htable AS HANDLE.
DEFINE VARIABLE hbuffer AS HANDLE.
DEFINE VARIABLE hquery AS HANDLE.
defINE VARIABLE httable AS HANDLE.
DEFINE VARIABLE hfield AS handle extent 3.
DEFINE VARIABLE display_field as char form "x(20)" extent 3.
DEFINE VARIABLE this_loop as int.

def temp-table mytable
field field1 as char
field field2 as char
field field3 as char
index idx1 field1.

/* Get some data into test temp-table */
create mytable.
mytable.field1 = "1.1".
mytable.field2 = "1.2".
mytable.field3 = "1.3".
create mytable.
mytable.field1 = "2.1".
mytable.field2 = "2.2".
mytable.field3 = "2.3".
create mytable.
mytable.field1 = "3.1".
mytable.field2 = "3.2".
mytable.field3 = "3.3".

/* get table handles */
httable = BUFFER mytable:HANDLE.
/* create an empty undefined temp-table */
CREATE TEMP-TABLE htable.
/* give it order table's fields & indexes */
htable:CREATE-LIKE(httable).
htable:TEMP-TABLE-PREPARE("report").

/* get the buffer handle for the temp-table */
hbuffer = htable:default-BUFFER-HANDLE.
/* You could do this bit in a loop - but beware of fields with extents */
hfield[1] = hbuffer:BUFFER-FIELD("field1").
hfield[2] = hbuffer:BUFFER-FIELD("field2").
hfield[3] = hbuffer:BUFFER-FIELD("field3").

/* populate the temp-table from mytable */
FOR EACH mytable:
hbuffer:BUFFER-CREATE.
hbuffer:BUFFER-COPY(httable).
end.

/* run a query to access the temp-table */
CREATE QUERY hquery.
hquery:SET-BUFFERS(hbuffer).
hquery:QUERY-PREPARE("for each report").
hquery:QUERY-OPEN().
REPEAT:
hquery:GET-NEXT().
IF hquery:QUERY-OFF-END THEN LEAVE.
do this_loop = 1 to 3:
display_field [this_loop] = hfield [this_loop]:buffer-value().
end.
display display_field.
END.
hquery:QUERY-CLOSE().
hbuffer:BUFFER-RELEASE().
DELETE OBJECT htable.
DELETE OBJECT hquery.
 
The problem is that my dynamic temp-table field's have the same names as my statis temp-table fields, BUT it does not include all the fields of my static temp-table. can buffer-copy still work?
 
Tested it and is working.

I was going the wrong, long and hard way.
BUT It was so simple.

Many thanks.
 
Back
Top