Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question

closed as off topic by jcolebrand Mar 20 '12 at 14:51

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

This can be solved by looking at any number of web resources. This does not need the input of a database administrator. – jcolebrand Mar 20 '12 at 14:51

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


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)

share|improve this answer
One should add that the go statement (separating batches of statements) is not T-SQL and is only available in SSMS so the variable lasts as long as the batch. – jollarvia Dec 3 '15 at 15:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.