Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

temp tables in sproc problem

Status
Not open for further replies.

jepatte

Programmer
Jul 4, 2005
75
US
I need to create a temp table in a sproc but I an getting a strange error.

---------------------------------------
CREATE PROCEDURE [dbo].[sp_UpdateTotalsAgePrecinct]
(
@fromAge as Integer, @toAge as Integer
)
AS

IF @toAge > 0
SELECT NCOACounty, NCOAPrecinct INTO #tmpPrecinctAge
FROM dbo.AllData
ELSE
SELECT NCOACounty, NCOACountyCode INTO #tmpPrecinctAge
FROM dbo.AllData
---------------------------------------

I get the error #tmpPrecinctAge is already an object. The error is on the line that starts after the else. If I take out the Else and last sql statement, I can save it fine, but as is above, I can save this.
 
Did you have DROP TABLE #tmpPrecinctAge at the end of SP?

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Sorry, yes, I had a drop at the end. I still get "#tmpPrecinctAge is already an object". I get this message when I create the sproc though.
 
Try
Code:
CREATE PROCEDURE [dbo].[sp_UpdateTotalsAgePrecinct]
(
    @fromAge as Integer, @toAge as Integer
)
AS
IF OBJECT_ID('TempDB..#tmpPrecinctAge') IS NOT NULL
   DROP TABLE #tmpPrecinctAge

IF @toAge > 0
    SELECT NCOACounty, NCOAPrecinct INTO #tmpPrecinctAge
    FROM dbo.AllData
ELSE
    SELECT NCOACounty, NCOACountyCode INTO #tmpPrecinctAge
    FROM dbo.AllData
...

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
I just read an article that the same temp table can't be created in a sproc even if its in an if-else. I assume this is a limitation to at least SQL 2000 and before the sproc is compiled, there must be something in memory disallowing this.
 
Also the SELECT INTO syntax that you are using should pretty much always be avoided.
See the first part of:

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

My Blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top