Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I know that @table drops automatically. We need not worry about it. But I want reason, why can't we drop table variable explicitly?

What I have tried:

I have searched in Google but didn't find appropriate answer.
Posted
Updated 18-Nov-20 13:28pm
Comments
Santosh kumar Pithani 20-Nov-17 1:58am    
https://stackoverflow.com/questions/11857789/when-should-i-use-a-table-variable-vs-temporary-table-in-sql-server
Santosh kumar Pithani 20-Nov-17 1:59am    
Don't post like this questions ones more...
HarshadaBS 9-Dec-17 9:47am    
@santosh Kumar sir .. Why? I searched everywhere but everyone gives complicated answers which is difficult to understand. That's why I posted here
Santosh kumar Pithani 10-Dec-17 22:58pm    
Variable tables(@table) is not scheme objects(tables,stored procedures..etc) to store permanently in Database so we cannot drop it.Its a very simple question to find answer easily. Example :-Declare @a int=12 --Here we can't drop a variable but we can read it very time in sql script.
Suvendu Shekhar Giri 20-Nov-17 2:42am    
You can't DROP a table variable as it's still a variable. These are also not affected by TRANSACTIONS.

As mentioned in the comments, it is a variable and cannot be dropped. It is not stored in the same place. The simple answer is "because it is a variable."
 
Share this answer
 
Comments
HarshadaBS 9-Dec-17 9:46am    
Yes. I got it.
The explanation is quite simple as the solution. You cannot execute or get the variable recongnized because when you type drop table @yourcreatedvariable, you are actually trying to delete a temp table that I suppose it is not the case.

Try this

DECLARE @myvar NVCHAR(50), @dropStatement NVCHAR(100)
SET @myvar = 'yourTableName'
SET @dropStatement = 'drop table ' + @myvar

exec sp_sqlexec @dropStatement

Jose
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900