Solved

How can I create a temp table and insert data from a query easily?

Posted on 2009-07-08
3
201 Views
Last Modified: 2012-05-07
I'd like to insert data from a query into a temp table in the simplest manner.  I've looked at INSERT INTO SELECT, but it looks like the source data has to be from a table.  There's got to be a way to do this without cursors, right?  Thanks!
0
Comment
Question by:jvalescu
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 24806332
Hello jvalescu,

The quick and dirty way:

SELECT
INTO #temp_table_name
FROM
WHERE

Otherwise, first you have to use CREATE TABLE to make the temp table, and then an INSERT INTO to
populate the temp table.  However, the CREATE/INSERT approach allows complete control over the table
design.

Regards,

Patrick
0
 

Author Closing Comment

by:jvalescu
ID: 31601240
Thanks!
0
 
LVL 42

Expert Comment

by:dqmq
ID: 24806578
>but it looks like the source data has to be from a table.

Not true.  the query will act just like a table:

Select * into #temp_table
from (select ... your query goes here)
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

708 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

17 Experts available now in Live!

Get 1:1 Help Now