Previous Topic

Next Topic

Declare Global Temporary Table

Valid in: SQL, ESQL

The Declare Global Temporary Table statement creates a temporary table.

Previous Topic

Next Topic

Syntax

The Declare Global Temporary Table statement has the following format:

[EXEC SQL] DECLARE GLOBAL TEMPORARY TABLE [SESSION.]table_name
              (column_name format {, column_name format})
              ON COMMIT PRESERVE ROWS
              WITH NORECOVERY
              [with_clause]

To create a temporary table by selecting data from another table:

[EXEC SQL] DECLARE GLOBAL TEMPORARY TABLE [SESSION.]table_name
              (column_name {, column_name})
              AS subselect
              ON COMMIT PRESERVE ROWS
              WITH NORECOVERY
              [with_clause]

Previous Topic

Next Topic

Description

The Declare Global Temporary Table statement creates a temporary table, also referred to as a session-scope table. Temporary tables are useful in applications that need to manipulate intermediate results and want to minimize the processing overhead associated with creating tables.

Temporary tables have the following characteristics:

The SESSION schema qualifier on the table name is optional, as described in SESSION Schema Qualifier.

If the location parameter is omitted, the temporary table is located on the default database location (if the temporary table requires disk space). If the subselect is omitted, the temporary table is created as a heap.

When a transaction is rolled back, any temporary table that was in the process of being updated is dropped (because the normal logging and recovery processes are not used for temporary tables).

Note: If II_DECIMAL is set to comma, be sure that when SQL syntax requires a comma (such as list of table columns or SQL functions with several parameters), that the comma is followed by a space. For example:

select col1, ifnull(col2, 0), left(col4, 22) from ti:

Previous Topic

Next Topic

SESSION Schema Qualifier

Two syntaxes can be used for declaring and referencing global temporary tables:

Note: In both modes, a session table is local to the session, which means that two sessions can declare a global temporary table of the same name and they do not conflict with each other.

Note: Syntaxes cannot be mixed in a single session. For example, if the table is declared with SESSION the first time, all declarations must use SESSION.

Previous Topic

Next Topic

Embedded Usage

In an embedded Declare Global Temporary Table statement:

Previous Topic

Next Topic

Permissions

All users are permitted to use this statement.

Previous Topic

Next Topic

Restrictions

Temporary tables are subject to the following restrictions:

All SQL statements can be used with temporary tables except the following:

The following commands cannot be issued with a temporary table name:

Previous Topic

Next Topic

Related Statements

Create Table

Delete

Drop

Insert

Select (interactive)

Update

Previous Topic

Next Topic

Examples: Declare Global Temporary Table

The following are Declare Global Temporary Table statement examples:

  1. Create a temporary table.

    exec sql declare global temporary table
        emps
        (name char(20) , empno char(5))
        on commit preserve rows
        with norecovery,
        location = (personnel),
        [no]duplicates,
        allocation=100,
        extend=100;

  2. Use a subselect to create a temporary table containing the names and employee numbers of the highest-rated employees.

    exec sql declare global temporary table
        emps_to_promote
        as select name, empno from employees
        where rating >= 9
        on commit preserve rows
        with norecovery


© 2007 Ingres Corporation. All rights reserved.