Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using temporary tables in Stored Procedures

Posted on 2004-09-22
3
Medium Priority
?
228 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 1200 total points
ID: 12127248
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 400 total points
ID: 12127270
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
ID: 12127364
Thanks guys it works.  I totally apreciate your help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

926 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