DROP TABLE @VariableName
It seems T-Sql has some rules about which commands can pass
variables, and which cannot. DROP <anything> seems to not allow variable
passing... Say, inside a sp one wanted to use a _While_ loop to store a
variable name - and later use it, example:
-- While loop variables
DECLARE @lni INT
SET @lni = 0
-- Loop Through Import Tables and Drop Same.
WHILE @lni < 6 BEGIN
-- Increment variable for While Loop
SET @lni = ( @lni + 1 )
-- Which Table to Drop?
BEGIN SET @lcTable =
CASE WHEN @lni = 1 THEN 'Table_This'
WHEN @lni = 2 THEN 'Table_That'
WHEN @lni = 3 THEN 'Table_TheOtherThing'
END -- Case Statement
DROP TABLE @lcTable
What is the correct way to to use these commands, and pass a variable... I
have tried creating a string and executing it, like:
@SQL = "DROP TABLE " + @lcTable
EXEC &lcTable -- Cannot Find sp error returned....
EXEC sp_executesql &lcTable -- Error invalid column name, or
something equally as <not impressing me>
So, there must be another popular way?
John C. Gunvaldson
San Diego, CA
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center