Question How to create dynamic temp table

Febri bin Sahi

New Member
Huy guys, I have been trying to write a dynamic temp table. source code as below. but when execute no output is generated. anyone can help a friend?

Code:
DEF TEMP-TABLE t-arrangement LIKE arrangement.
DEF VAR table-name AS CHAR INIT "arrangement".
DEFINE VARIABLE hBuffer  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hfield  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hQuery  AS HANDLE  NO-UNDO.
RUN ipdump (table-name).
FOR EACH t-arrangement:
  DISP t-arrangement.
END.
PROCEDURE ipDump:
  DEFINE INPUT  PARAMETER picTable  AS CHARACTER  NO-UNDO.
 
  DEFINE VARIABLE iDumpCount AS INTEGER  NO-UNDO.
  DEFINE VARIABLE str-query AS CHAR.
  str-query = "for each " + picTable + " no-lock".
  CREATE BUFFER hbuffer FOR TABLE pictable.
  CREATE QUERY hquery.
  hquery:SET-BUFFERS(hBuffer).
  hquery:QUERY-PREPARE(str-query).
  hquery:QUERY-OPEN.
  hquery:GET-FIRST.
  REPEAT:
  idumpcount = idumpcount + 1.
  IF hquery:QUERY-OFF-END THEN LEAVE.
  IF iDumpCount > 100 THEN LEAVE.
  RUN ipDumpField.
  hquery:GET-NEXT.
  END.
 
END PROCEDURE.
PROCEDURE ipDumpField:
  DEFINE VARIABLE iField  AS INTEGER  NO-UNDO.
  DEFINE VARIABLE iextent  AS INTEGER  NO-UNDO.
  DEFINE VARIABLE cquote  AS CHARACTER  NO-UNDO.
 
  _FIELD_LOOP:
  DO ifield = 1 TO hBuffer:NUM-FIELDS.
  ASSIGN hfield = hbuffer:BUFFER-FIELD (iField).
 
  IF hfield:DATA-TYPE = "character" THEN
  cQuote = """".
  ELSE
  cquote = "".
  IF hfield:EXTENT = 0 THEN
  /*PUT STREAM sDump UNFORMATTED cquote hfield:BUFFER-VALUE cquote " ".*/.
  ELSE DO iextent = 1 TO hfield:EXTENT:
  /*PUT STREAM sDump UNFORMATTED cquote hfield:BUFFER-VALUE (iextent) cquote " ".*/.
  END.
  END.
 
END PROCEDURE.
 
Last edited:

RealHeavyDude

Well-Known Member
Please wrap your code in code tags - it improves the readability a lot ...

As far as I can see you are passing in "arrangement" to your internal procedure whereas your temp-table is named t-arrangement ...

Heavy Regards, RealHeavyDude.
 

Cringer

ProgressTalk.com Moderator
Staff member
It might be a good idea if you can tell us what you are trying to achieve because the code above is a little confused. Are you trying to create a dynamic table that is identical in structure and contents to the arrangement database table?
 

Cringer

ProgressTalk.com Moderator
Staff member
I'm not going to solve the problem for you, but I will give you some snippets of semi-pseudo-code that should help you.
Code:
CREATE TEMP-TABLE lv-TTHandle.
Loop Through DB Table Fields:
  lv-TTHandle:ADD-LIKE-FIELD(buffer name,buffer field).
END. 
lv-TTHandle:TEMP-TABLE-PREPARE("Your TT Name").
lv-DefaultBufferHandle = lv-TTHandle:DEFAULT-BUFFER-HANDLE.

Loop Through DB Records:
  lv-DefaultBufferHandle:BUFFER-CREATE.
  buffer-copy db record to default buffer handle. 
end. 

/*Now you can do a dynamic query to see your TT records*/

CREATE QUERY lv-BrowseQuery.                  
   
ASSIGN lv-Ok = lv-BrowseQuery:SET-BUFFERS(lv-TTHandle:DEFAULT-BUFFER-HANDLE).
ASSIGN lv-Query = "FOR EACH " + ttname + " NO-LOCK".
ASSIGN lv-Ok = lv-BrowseQuery:QUERY-PREPARE(lv-Query).
ASSIGN lv-Ok = lv-BrowseQuery:QUERY-OPEN().  
DO WHILE lv-BrowseQuery:GET-NEXT():
  Whatever.
END.


I hope that helps.
 

TomBascom

Curmudgeon
I'd imagine that it might also have something to do with the fact that the PUT statements are commented out. And I don't see any way for there to ever be any data in the temp-table in the first place -- I might have missed it but don't see any records being created, so I wouldn't expect there to be any output.
 

Cringer

ProgressTalk.com Moderator
Staff member
I suspect the PUT statements are there to verify that something is actually happening. I could be wrong. But yes, the only thing being created is a new buffer to the db table.
 

Febri bin Sahi

New Member
ok, thank you for diskusinya. But I do not understand how the concept of dynamic temp table. I want to create a dynamic temp table which coordinate many tables inside.
And if I execute in BL, the table will be created automatically in UI.

Best Regards..
 

RealHeavyDude

Well-Known Member
I don't understand what is not to understand with the concept of a dynamic temp-table. Basically they are the same than static temp-tables with the exception that the are resolved at run time whereas static ones are resolved at compile time. You define a static or create a dynamic temp-table. Then you use it - probably populating it with data from various sources. And, when you are done you empty it and, if it is a dynamic one, delete it.

Plus, don't mistake the handle to a static temp-table with the handle to a dynamic buffer. If you are not familiar with the difference of a database table or a temp-table and the buffer(s) associated with them, you should begin with understanding that concept: Basically database tables and temp-tables hold the data and you use buffers to access them. All of them come with a default buffer associated with them which name is the same as the table. But you can use additional defined buffers ( static ) or dynamically create buffer objects at run time to access them. Escpecially when updating static temp-tables you should always use at least defined buffers ( buffer scope is the key here ).

But, if you want to arrange data of several different tables in a temporary structure the ProDataSet might be better suited.

Heavy Regards, RealHeavyDude.
 
Top