Temporary tables

Use temporary tables when you need to store data for only the duration of an application process. Depending on whether you want to share the table definition, you can create a created temporary table or a declared temporary table.

The two kinds of temporary tables are:
  • Created temporary tables, which you define using a CREATE GLOBAL TEMPORARY TABLE statement
  • Declared temporary tables, which you define using a DECLARE GLOBAL TEMPORARY TABLE statement
SQL statements that use temporary tables can run faster because of the following reasons:
  • For created temporary tables, Db2 provides no logging. For declared temporary tables, Db2 provides limited logging that can be further limited by the NOT LOGGED option of the DECLARE GLOBAL TEMPORARY TABLE statement.
  • For created temporary tables, Db2 provides no locking. For declared temporary tables, Db2 provides limited locking.

Temporary tables are especially useful when you need to sort or query intermediate result tables that contain a large number of rows, but you want to store only a small subset of those rows permanently.

Temporary tables can also return result sets from stored procedures. The following topics provide more details about created temporary tables and declared temporary tables: For more information, see Writing an external procedure to return result sets to a distributed client.