Solved

ORACLE How to SELECT INTO a TEMP TABLE

Posted on 2015-01-15
6
3,607 Views
Last Modified: 2015-01-16
Hello,

I want to SELECT into a TEMP TABLE.  The Temp table DOES NOT EXIST.  Can I create a temp table on the fly?

For example:

SELECT *
INTO TEMP_TABLE (doesn't yet exist)
FROM....
WHERE....

IS this possible in Oracle SQL?

Thanks!
0
Comment
Question by:dunkin1969
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 400 total points
ID: 40552588
CREATE GLOBAL TEMPORARY TABLE TEMP_TABLE 
ON COMMIT PRESERVE ROWS
AS
select * from existing_table

Open in new window

0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 100 total points
ID: 40552610
Although you 'can' use the syntax above to create it on the fly, it typically isn't done that way.

Using the above syntax, the data will be gone when you close the session but the table remains.

In Oracle a global temporary table is a permanent table.  The only thing that is temporary in them is the data.  Only the session that inserts data into them can see it and the data is automatically removed when the session ends.


Create it once and:
insert into INTO TEMP_TABLE
SELECT *
FROM ...
WHERE....
0
 

Author Comment

by:dunkin1969
ID: 40552626
Michael74,
Thanks I think that will work for me.  Even if the data is lost when session ends.

Slightw,
I assume you're saying to create my temp table first, then insert into it.  I was trying to prevent having to create the tables manually first.  I remember in SQL, all I had to do was SELECT INTO #TableName... I was hoping Oracle had something similar.

Thanks!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40552636
Temp tables in Oracle don't work like some other databases.

There isn't a transient temp table that magically goes away when you no longer need it.  Just the data.

You create the table, you must drop it when you are done.

>>I was trying to prevent having to create the tables manually first

You only create it once EVER.  It is never dropped.

It isn't:
create table.
use table.
drop table.

It is:
create table.
use table FOREVER AND ALWAYS (never dropped).
0
 

Author Comment

by:dunkin1969
ID: 40552689
Thanks for the explanation Slightw.

I don't care if the temp table is dropped after my session ends.  My purpose for using temp tables is to alleviate subqueries and make performance better on our slow server.  I just need the temp tables for that session.  Which is why I didn't need to create the table first (I hate having to do that just for something i only need on a temp basis)... especially if there are 100+ columns I'd have to create.  Much easier to use the temp table method like in the SQL i'm used to:

SELECT *
INTO #Temp
FROM
  (SELECT...)

But like I said, for my particular purpose, I will use the method suggested by Micheal74.  Thank you
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40553291
>>I don't care if the temp table is dropped after my session ends.

You should.  If you don't drop it, it will remain in the data dictionary forever.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
OSQL to execute sql command 26 24
SQL Server / Update DB? 22 36
where to find DTS instalation package for sql 2014 64 bit 3 10
Are triggers slow? 7 10
I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question