Creating temp tables with variable names

Zadock

New Member
Hi Everyone.

I want to create a temp-table whose name should be based on records found in a table.

For example:
for each customer break by country.
if first-of(Customer.Country) then do:
create temp-table value(country).
assign fields etc etc.
end.

The name to be given to the temp-table should be the name of the country.

Any ideas?

Regards

Zadock
 
Hi

You can do something along the lines of:

DEFINE VARIABLE hTable AS HANDLE NO-UNDO.
CREATE TEMP-TABLE hTable.
hTable:NAME = customer.country.
hTable:ADD-NEW-FIELD("code" /* field name */, "character").
hTable::code = /* what do we assign in the field(s) here? */

You do not tell enough about the fields in the temp-tables for a more comprehensive answer...

You also might want to maintain the table handles in a temp-table so that you can find the handle by name or code later.

++

JC

PS: hTable::code can also be written hTable:BUFFER-FIELD("code"):BUFFER-VALUE (in case "code" cannot be hard-coded)
 

Zadock

New Member
Hi JC,

Many thanks for your response.

Once all the table are created, the field "code" should contain the name of the country as well.

Later in my program, I want to call the temp-table "USA" that we have just created, and display the field "code" which in this case should display "USA" as well.

Kindly assist me with a short code then I will be able to build on it from there.

Thanks in advance.

Regards

Zadock
 
Well, it seems overly complicated but the following is doing something which might help:

Code:
DEFINE TEMP-TABLE ttTempTables NO-UNDO
    FIELD cCode AS CHARACTER
    FIELD hTable AS handle
    FIELD hBuffer AS HANDLE
    INDEX ix IS PRIMARY UNIQUE cCode.

for each customer break by country:
    if first-of(Customer.Country) then do:
        CREATE ttTempTables.
        ttTempTables.cCode = Customer.Country.

        CREATE TEMP-TABLE ttTempTables.hTable.
        ttTempTables.hTable:ADD-NEW-FIELD("code", "CHARACTER").
        ttTempTables.hTable:ADD-NEW-FIELD("qty", "INTEGER").
        ttTempTables.hTable:TEMP-TABLE-PREPARE("tt" + REPLACE(customer.country, " ", "_")).
        ttTempTables.hBuffer = ttTempTables.hTable:DEFAULT-BUFFER-HANDLE.
        ttTempTables.hBuffer:BUFFER-CREATE().
        ttTempTables.hBuffer::code = Customer.Country.
    END.
    ttTempTables.hBuffer::qty = ttTempTables.hBuffer::qty + 1.
END.

FOR EACH ttTempTables:
    DISP ttTempTables.cCode integer(ttTempTables.hTable).
    ttTempTables.hBuffer:FIND-FIRST().
    DISP ttTempTables.hBuffer::code ttTempTables.hBuffer::qty.
END.

Of course the same could be achieved without creating temp-tables dynamically...

What are you exactly trying to achieve?

PS: the above could (should) be done statically like this:
Code:
DEFINE TEMP-TABLE ttCountries NO-UNDO
    FIELD cCode AS CHARACTER
    FIELD qty AS INTEGER
    INDEX ix IS PRIMARY UNIQUE cCode.

for each customer break by country:
    if first-of(Customer.Country) then do:
        CREATE ttCountries.
        ttCountries.cCode = Customer.Country.
    END.
    ttCountries.qty = ttCountries.qty + 1.
END.

FOR EACH ttCountries:
    DISP ttCountries.cCode ttCountries.qty.
END.
The more complex example above only serves if there is more to it, which for the moment I cannot imagine ;)
 
Last edited:
Top