Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DROP multiple tables 1

Status
Not open for further replies.

richardy

IS-IT--Management
Oct 24, 2002
397
GB
Hi,

Is there a mechanism to use the DROP TABLE with multiple tables in one go ie V% or something similar?

Regards,

Richard
 
something like (untested)

Code:
declare @owner sysname
declare @table sysname
declare @cmd nvarchar(500)

declare tcur cursor for
select TABLE_SCHEMA,TABLE_NAME
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME like 'TEST%'
and TABLE_TYPE = 'BASE TABLE'
and OBJECTPROPERTY(OBJECT_ID(TABLE_NAME),'IsMSShipped')=0

open tcur
fetch next from tcur into @owner,@table
while @@fetch_status = 0
begin

   set @cmd = N'DROP TABLE ['+@owner+N'].['+@table+N']'
   exec(@cmd)

   fetch next from tcur into @owner,@table
end

close tcur
deallocate tcur

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Try:
Code:
exec sp_msForEachTable 'drop table ?', @whereand= ' and name like ''v%'' '
And of course... do it carefully.


------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
As vongrunt mentioned, you should use automated scripts carefully when doing something like dropping a table.

Personally I would prefer to use something like the code below (rather than automate the process):

Run the statement:
Code:
select 'Drop Table ' + table_name from information_schema.tables where table_name like 'v%'

Double check the results and run the drop scripts.

Regards,
AA



 
fredericofonseca, perfectly timed post.

I needed to be able to create tables where the name is unknown (ie based on a series of variables) and this :

set @cmd = N'DROP TABLE ['+@owner+N'].['+@table+N']'
exec(@cmd)

was the exact example I needed.

Thanks.



[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top