You are on page 1of 2

Global temporary table example

Oracle Tips by Burleson Consulting

Question: I need an example of how a global temporary table works. How does a
global temporary table differs from a traditional temporary table or a WITH clause? I
need a working example of a global temporary table. Do you need to drop the Global
temporary table after each use?
Answer: When using a global temporary table you need only define the table once
for the entire schema and then any user can insert directly into the table without the
need to re-create the global temporary table.
drop table t1;
create table
t1
as
select sum(quantity) all_sales from stores;
Instead, a global temporary table does not required that the table be dropped and recreated and the global temporary tables are shared, such that each user gets a local
copy of their rows, using the common definition.
create global temporary table
sum_quantity
(sum_qty number);
Once created, everyone shares the table definition and rows are private to each
session. Here is an example of using a global temporary table:
SQL> insert into sum_quantity
2 (select sum(qty) from sales);
Note that the rows disappear after the SQL statement has completed, so that a GTT
can be used over-and-over without redoing the table.
SQL> select * from sum_quantity;
SUM_QTY

---------499
SQL> commit;
Commit complete.
SQL> select * from sum_quantity;
no rows selected

You might also like