I have a query that's in development that uses several temp tables. In order to test the query repeatedly while it's being written I have the following code at the beginning to discard the temp tables. This allows the query can recreate the temp tables when called in the code.
if object_id('tempdb..#temp1') is not null drop table #temp1
if object_id('tempdb..#temp2') is not null drop table #temp2
if object_id('tempdb..#temp3') is not null drop table #temp3
if object_id('tempdb..#temp4') is not null drop table #temp4
if object_id('tempdb..#temp5') is not null drop table #temp5
Even though this works, it takes multiple lines of code. One of my queries has to drop 12 temp tables, thus 12 lines of code. I have been experimenting with looping the above as follows:
declare @.n as nvarchar(3), @.table as nvarchar(10)
set @.n = 1
set @.table = '#temp'+@.n
if object_id('tempdb..#temp'+@.n) is not null drop table @.table
set @.n = @.n + 1
Unfortunately, the above does not work. It gives this error message:
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near '@.table'.
I have also tried:
declare @.n as nvarchar(3), @.dropstmt as nvarchar(25)
set @.n = 1
set @.dropstmt = 'drop table #temp'+@.n
if object_id('tempdb..#temp'+@.n) is not null @.dropstmt
set @.n = @.n + 1
This does not work either. It gives this error message:
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near '@.dropstmt'.
Does anyone know how to get this to work?
Thanks.
Code Snippet
declare @.n as nvarchar(3),
@.table as nvarchar(10),
@.str nvarchar(1000)
set @.n = 1
while @.n <= 5 begin
set @.table = '#temp'+@.n
set @.str = 'if object_id('+''''+'tempdb..'+@.table+''''+') is not null drop table '+ @.table
print @.str
exec sp_executesql @.str
set @.n = @.n + 1
end
|||
Here it is,
Code Snippet
declare @.n as nvarchar(3), @.table as nvarchar(10)
set @.n = 1
while @.n <= 5 begin
set @.table = '#temp'+@.n
if object_id('tempdb..#temp'+@.n) is not null
exec('drop table ' + @.table)
set @.n = @.n + 1
end
|||Hi,
The following code will print the "DROP TABLE <Your Temp Tables> list of commands in your message tab", and you need to run those commands by copying all of them into your query editor and excute them all no need to write any loop statements.
Use TempDb
Go
DECLARE @.RETURN_VALUE int
DECLARE @.command1 nvarchar(2000)
DECLARE @.whereand nvarchar(2000)
SET @.command1 = 'Print ''drop table ?'''
SET @.whereand = 'AND O.Name LIKE ''#Temp[0-9]%'''
EXEC @.RETURN_VALUE = sp_MSforeachtable @.command1=@.command1,
@.whereand = @.whereand
Reference:
http://www.dbazine.com/sql/sql-articles/larsen5
Regards,
Kiran.Y
|||Dear Moderator,
Thank-you very much for the code snippet. This method worked beautifully. Just one question...why is it necessary to use
exec('drop table '+@.table)?
Why doesn't it recognize the plain old
drop table @.table
immediately following the "is not null" statement as written in my original attempt?
|||Thanks to everyone who replied and provided code snippets. It is very helpful to see those. It is also reassuring to know there is more than one possible solution.
|||
Because the DDL statements do not allow variables for commands. It is really kind of annoying at times, but DDL is really not made to execute dynamically like this, as it is expected that these are management constructs, not programming ones.
You don't actually have to drop temp tables, but if I was going to do it, I would use your initial form. It will likely be a little bit faster as you already have to do the same amount of work against the database, and easier to test as there is no looping variable based code that could go wrong (not that it has a large chance to, but the other method is safer.
If you know that all of the tables are created (and really you should usually know what tables you are going to create in your code that is using a temp table,) then you could just do:
drop table table1, table2, table3, etc.
|||One further note about about this -
The final code I arrived at is as follows:
Code Snippet
declare @.n as nvarchar(3)
set @.n = 1
while @.n <= 5 begin
if object_id('tempdb..#temp'+@.n) is not null
exec('drop table #temp'+@.n)
set @.n = @.n + 1
end
This is simplified one more step by removing the @.table variable and it seems to work fine.
Thanks for everyone's help on this.
sql
No comments:
Post a Comment