Temporary tables
Syntax for temporary table creation is not unique across all database engines.
Not all database servers support temporary tables. The engines supporting this feature often provide it with a specific table creation statement:
Database Server Type | Native temp table creation syntax | Temp table support |
---|---|---|
IBM® DB2® LUW |
Note: DB2 Version 11.1 supports CREATE TEMP TABLE syntax for compatibility with Netezza.
|
Emulated, see details |
IBM Informix® |
|
Yes, native SQL feature |
IBM Netezza |
|
Emulated, see details |
Microsoft™ SQL Server |
|
Emulated, see details |
Oracle® MySQL / MariadDB |
|
Emulated, see details |
Oracle Database Server |
Note: With ORACLE global temporary tables, only the data is local to the SQL session: The table is
shared among all programs.
|
Emulated, see details |
PostgreSQL |
|
Emulated, see details |
SAP HANA® |
|
Emulated, see details |
SQLite |
|
Emulated, see details |
Some databases even have a different behavior when using temporary tables. For example, Oracle 9i supports a kind of temporary table, but it must be created as a permanent table. The table is not specific to an SQL session: it is shared by all processes - only the data is local to a database session.
Simple Informix-style SQL statement creating temporary tables can be converted to a native SQL
equivalent instruction. However, complex SQL statements such as SELECT .. INTO TEMP
with subqueries may fail. In such case, create a view from the complex query and then create the
temp table from the view. Or, disable Informix emulation and use the native SQL syntax to create the
temporary table (EXECUTE IMMEDIATE "/* fglhint_no_ifxemul */ …"
)
With Informix SQL, if the source table has a column defined as SERIAL
or
BIGSERIAL
, a SELECT ... INTO TEMP
will produce a new temp table
with an auto-incremented serial column. With the SELECT … INTO TEMP
emulation for
non-Informix databases, not using the native sequence generators (such as IDENTITFY
columns in SQL Server), the resulting temporary table will get a simple INTEGER
or
BIGINT
column, instead of an auto-incremented column.
Consider reviewing programs using temporary tables, and adapt the code to create temporary tables with native SQL syntax.