A temporary table (temp-table) is a very important ABL construct that allows you to define a set of data. Temp-tables are relational-based structures, visible to the OpenEdge session that creates them, and only last the duration of the session. Temp-tables may represent data from one or more tables in your database or they may contain different data.

Use cases

There are two main use cases for temp-tables. You can use a temp-table to represent data from one or more tables in your database. In this use case you perform similar operations on the temp-table, as you would on a database table, however the database itself is not affected. This allows operations to take place in the application logic, without any involvement from the database server. You initially fill the temp-table with data from one or more tables in the database, You can then manipulate the data in the temp-table without tying up the database. The database can be updated with changes from the temp-table at a later time, although care must be given to data integrity since the database might have changed during this time.

The second use case is when you want to work with a set of local data and even pass this set of data to another procedure or session. You can think of a temp-table in this case as a columnar structure where each row uses the same local schema definition.

Static temp-tables

Static temp-tables are used when the schema of the table is known during development. You define a static temp-table using the DEFINE TEMP-TABLE statement to create the schema for the temp-table. You create a record in the temp-table using the CREATE statement. The following example code defines a temp-table called ttCustomer with two fields (columns), creates a record, assigns values to the fields, and displays the record.
DEFINE TEMP-TABLE ttCustomer NO-UNDO     // Define the temp-table
  FIELD CustName AS CHARACTER
  FIELD CustId AS CHARACTER. 
 
CREATE ttCustomer.     // Create a record 
CustName = "John Smith".     // Assign values to the fields
CustId = "98765". 

DISPLAY ttCustomer.     // Display the record

You can pass a static temp-table to another procedure using the TABLE parameter. In this case the table data is copied from one procedure to the other. Static temp-tables require a complete, static definition of the table on each side of the transfer, because the schema is not passed as part of the parameter. For more information see Using a temp-table as a parameter.

Dynamic temp-tables

Dynamic temp-tables are used when the schema of the table is not known until runtime. You use the CREATE TEMP-TABLE statement to create an empty temp-table at runtime. You then define the schema using ADD methods such as ADD-NEW-FIELD( ) and ADD-NEW-INDEX( ). Once the definition for the dynamic temp-table is complete, you call TEMP-TABLE-PREPARE( ) to signal that the table definition is complete. The temp-table is now ready to hold data.

The following example code demonstrates creating a dynamic temp-table.

DEFINE VARIABLE tth as HANDLE NO-UNDO.
DEFINE VARIABLE bufferh as HANDLE NO-UNDO.

/* Create an empty, undefined temp-table */
CREATE TEMP-TABLE tth.

/* Add fields to the temp-table */
tth:ADD-NEW-FIELD("custName","character").
tth:ADD-NEW-FIELD("custNum","integer").

/* Signal that the temp-table definition is complete and assign it the name "ttCust" */
tth:TEMP-TABLE-PREPARE("ttCust").

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

/* Create a record and store it in the buffer */
bufferh:BUFFER-CREATE().

/* Assign values */
bufferh:BUFFER-FIELD("custName"):BUFFER-VALUE = "John Smith".
bufferh:BUFFER-FIELD("custNum"):BUFFER-VALUE = "12345".
...

To pass a dynamic temp-table you pass the handle itself. You can also pass a dynamic temp-table using the TABLE-HANDLE parameter. For more information see Parameter passing syntax.

See also

Defining and Using Temp-tables

Temp-table object handle