Take the 2-minute tour ×
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. It's 100% free, no registration required.

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.

2  
Please work on your acceptance rate by accepting answers to your prior questions if they helped you. –  Thomas Stringer Mar 20 '12 at 14:38
2  
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
add comment

1 Answer 1

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)

share|improve this answer
add comment

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