Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Updating a temp table in a Stored Proc

4.00/5 (1 vote)
1 Nov 2011CPOL 6.6K  
You might also want to try using a table variable - like this:DECLARE @tableName TABLE ( Key1 INT, Key2 INT, Description NVARCHAR(255), AggAmount FLOAT,)Then you can refer to your table by @tableName. Granted this is SQL Server 2005 and above, but then...

You might also want to try using a table variable - like this:


SQL
DECLARE @tableName TABLE  ( 
    Key1        INT,
    Key2        INT,
    Description NVARCHAR(255),
    AggAmount   FLOAT,
)

Then you can refer to your table by @tableName. Granted this is SQL Server 2005 and above, but then you don't have to worry about making sure your temp table exists or is not in tempdb.


The other option is, if it is a temp table, just delete it at the end of your Stored Procedure so it doesn't exist in tempdb anymore and release resources to SQL Server. Then if you change the schema in the beginning of your procedure, it doesn't matter.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)