Sql-server – How to drop variable in SQL Server

sql server

I have declare a variable in SQL Server: Declare @variable

How can I drop or enable the variable when I don't need it?

Best Answer

The scope of a variable lasts for the duration of when it is declared to the end of the batch or stored procedure. So in other words if you have something like this:

declare @someVar varchar(100)
select @someVar = 'this is a test'    -- this is legal

go

select @someVar = 'this will not work'    -- the variable @someVar is out of scope

See the MSDN Reference on T-SQL 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.

In other words, you don't explicitly drop variables like you do for other database objects. (the same goes for table variables as well)