Solved

Using temporary tables in Stored Procedures

Posted on 2004-09-22
3
216 Views
Last Modified: 2008-02-01
Hello everyone that can help me,

i am trying to create a stored procedure that uses a temporary table.  I want to be able to Use a Select Into statement to populate information into a tempory table.  

Then I want to run another Select Into statement to populate the same tempory table.  Then view the results of that tempory table.  It seems easy enough, through all of the books that I have on the subject, I am somewhat confused how to make it work in real life.

So far this is what I have:

Create Procedure dbo.usp_test AS
Select Account
Into #temptable
From Example_Table1
Go
Select Account
Into #temptable
From Example_Table2
Go
Select * from #temptable

Obviously it doesn't work.  As you can probably see, my experience with Procedures is limited. Can anyone help me with this? please help.
0
Comment
Question by:franksale
3 Comments
 
LVL 9

Accepted Solution

by:
paelo earned 300 total points
Comment Utility
I don't think you can perform subsequent SELECT INTO operations for the same temp table name.  You could try SELECT followed by INSERT:

Select Account
Into #temptable
From Example_Table1
Go
Insert Into #temptable
Select Account
From Example_Table2
Go


But you are probably better off explicity declaring your temporary table:


CREATE TABLE #temptable (
 Account varchar(20) --or whatever
)

Insert Into #temptable
Select Account
From Example_Table1
GO
Insert Into #temptable
Select Account
From Example_Table2
GO

DROP TABLE #temptable --not really necessary, local temps are dropped at the end of the proc


-Paul.
0
 
LVL 8

Assisted Solution

by:MartinCMS
MartinCMS earned 100 total points
Comment Utility
Create Procedure dbo.usp_test AS

Insert into temptable    (Account)
Select Account From Example_Table1

Insert into temptable    (Account)
Select Account From Example_Table2

Select * from temptable

Go
0
 
LVL 1

Author Comment

by:franksale
Comment Utility
Thanks guys it works.  I totally apreciate your help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 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

8 Experts available now in Live!

Get 1:1 Help Now