How do I drop table variables in SQL-Server? Should I even do this?
Asked Answered
M

8

161

I have a table variable in a script (not a stored procedure). Two questions:

  1. How do I drop the table variable? Drop Table @varName gives an "Incorrect snytax" error.
  2. Should I always do this? I hear it's a good practice. Is it ever really necessary for small scripts like this?

Here's my code:

Declare @projectList table(
    name varchar(40) NOT NULL);

Insert Into @projectList
Values ('BCR-00021')

Select *
From @projectList

Drop Table @projectList -- does not work
Meshed answered 13/4, 2011 at 18:0 Comment(1)
You can't drop them yourself so the 2nd part of your question doesn't apply.Halmahera
B
236

Table variables are automatically local and automatically dropped -- you don't have to worry about it.

Blindfish answered 13/4, 2011 at 18:1 Comment(6)
+1 - Also you can't drop them even if you wanted to - they persist as long as the session is open, just like any other variable. They are also unaffected by transactions.Assiduous
@JNKs point about them being unaffected by transactions is important, you can use table variables to hold data and write to a log table after an error causes a rollback.Lulualaba
No it is not the same. Temp tables participate in transactions.Battology
Nor are they the same as CTEs.Blindfish
you can't drop them but you can delete them 'delete @projectList' ;)Lemley
Just to be clear DELETE @projectList will remove all rows from the table in table variable @projectListBlindfish
O
46

if somebody else comes across this... and you really need to drop it like while in a loop, you can just delete all from the table variable:

DELETE FROM @tableVariableName
Onondaga answered 2/8, 2017 at 8:28 Comment(2)
but id will increment, it will not set to 1 again when you declare in loopTrefler
This will delete all rows in Table variable so it is empty for next iteration in the loop. This works for my requirement. Thank you.Tobietobin
B
32

Table variables are just like int or varchar variables.

You don't need to drop them. They have the same scope rules as int or varchar variables

The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.

Brack answered 13/4, 2011 at 18:4 Comment(1)
Do you know whether the tempdb objects created by table variables get cleaned up when they go out of scope? Or does the server wait until the session is closed before cleaning them up?Consubstantiation
G
12

But you all forgot to mention, that if a variable table is used within a loop it will need emptying (delete @table) prior to loading with data again within a loop.

Gezira answered 27/4, 2017 at 21:11 Comment(0)
R
3

Just Like TempTables, a local table variable is also created in TempDB. The scope of table variable is the batch, stored procedure and statement block in which it is declared. They can be passed as parameters between procedures. They are automatically dropped when you close that session on which you create them.

Remainder answered 15/5, 2015 at 7:1 Comment(1)
#temp table isn't the same as @table variable, it doesn't drop automatically after the end of patch or scope, it's dropped automatically only if it was created inside stored procedure and stored procedure finished executingGasometer
D
1

Temp table variable is saved to the temp.db and the scope is limited to the current execution. Hence, unlike dropping a Temp tables e.g drop table #tempTable, we don't have to explicitly drop Temp table variable @tempTableVariable. It is automatically taken care by the sql server.

drop table @tempTableVariable -- Invalid
Discommodity answered 29/9, 2020 at 21:20 Comment(0)
T
0

Indeed, you don't need to drop a @local_variable.

But if you use #local_table, it can be done, e.g. it's convenient to be able to re-execute a query several times.

SELECT *
INTO #recent_records
FROM dbo.my_table t
WHERE t.CreatedOn > '2021-01-01'
;

SELECT *
FROM #recent_records
;

/*
  can DROP here, otherwise will fail with the following error
  on re-execution in the same window (I use SSMS DB client):

  Msg 2714, Level ..., State ..., Line ...
  There is already an object named '#recent_records' in the database.
*/
DROP TABLE #recent_records
;

You can also put your SELECT statement in a TRANSACTION to be able to re-execute without an explicit DROP:

BEGIN TRANSACTION

  SELECT *
  INTO #recent_records
  FROM dbo.my_table t
  WHERE t.CreatedOn > '2021-01-01'
  ;

  SELECT *
  FROM #recent_records
  ;

ROLLBACK
Trek answered 12/1, 2021 at 17:15 Comment(0)
B
-1

Here is a solution

Declare @tablename varchar(20)
DECLARE @SQL NVARCHAR(MAX)

SET @tablename = '_RJ_TEMPOV4'
SET @SQL = 'DROP TABLE dbo.' + QUOTENAME(@tablename) + '';

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@tablename) AND type in (N'U'))
    EXEC sp_executesql @SQL;

Works fine on SQL Server 2014 Christophe

Boysenberry answered 11/4, 2019 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.