?
Solved

Using temporary tables in Stored Procedures

Posted on 2004-09-22
3
Medium Priority
?
225 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
[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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
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.

719 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