Solved

ORACLE How to SELECT INTO a TEMP TABLE

Posted on 2015-01-15
6
4,886 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 77

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 77

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 77

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to take different types of Oracle backups using RMAN.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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