Solved

ORACLE How to SELECT INTO a TEMP TABLE

Posted on 2015-01-15
6
5,732 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:
Michael Fowler 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

705 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