|
-
DROP TABLE @VariableName
Q?
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
BEGIN
DROP TABLE @lcTable
END
etc...
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?
tia
John C. Gunvaldson
San Diego, CA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks