[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ORACLE How to SELECT INTO a TEMP TABLE

Posted on 2015-01-15
6
Medium Priority
?
10,742 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:
Michael Fowler earned 1600 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 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 400 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 78

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 78

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

Independent Software Vendors: 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

Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
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.
Suggested Courses

829 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