how to create global temporary table in oracle

A temporary table has a definition and structure like that of a regular table, except that the data only exists for the duration of the current transaction or session.

CREATE GLOBAL TEMPORARY TABLE temp_table
     (a number,
      b varchar2(10)
     );

Temporary table is cleaned up atomically at session end or transaction end.

   INSERT INTO temp_table
   VALUES (1,'ABC');

If you fire select statement, it will display data.

SELECT * FROM temp_table;

OUTPUT
-------------------
A   B
---------------
1   ABC

Once you commit, the data from temporary table is deleted.

COMMIT;

If you fire select statement, it will not display any data.

SELECT * FROM temp_table;

i.e., in same session once the data is committed it will delete data
from temporary table.

The default behaviour of temporary table is ON COMMIT DELETE ROWS, which means that rows are visible only within the transaction.

ON COMMIT PRESERVE ROWS - The rows are visible after the transaction, but within the session only.

create global temporary table temp1(a number, b varchar2(5))
     ON COMMIT PRESERVE ROWS;

 

No comments:

Post a Comment