Solved

ORACLE How to SELECT INTO a TEMP TABLE

Posted on 2015-01-15
6
1,918 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now