Solved

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

Posted on 2009-07-08
3
206 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
[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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

752 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