Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to append to #tempTable

Author  Topic 

sarahmfr
Posting Yak Master

214 Posts

Posted - 2012-06-15 : 15:04:43
Hello all,
I am having a stored procedure
that pulls information from several tables and I want to populate those to one temporary table
Now I am using union
example
select forumtitle as tile ,
opendate as start,
closedate as [end]

from forum.threads
where xxx
union
select journaltitle as title,
opendate as start,
closedate as end
from journal.sectionjournat
where xxxx
and so on
how can I do that using #temptable

Thanks


sarah

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-15 : 15:19:17
Just add INSERT INTO #temptable to the top of your query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-15 : 16:10:57
is table already present or you want to create it on the fly?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2012-06-15 : 19:27:39
If the temp table does not exist yet and you want it created by your SELECT statement, just put the INTO #temptable before the first FROM clause:

SELECT Col1, Col2
INTO #TempTable
FROM Table1
UNION
SELECT Col1, Col2
FROM Table2

SQL Server Helper
http://www.sql-server-helper.com/tips/tip-of-the-day.aspx
Go to Top of Page
   

- Advertisement -