21 September 2014

SQL : Drop Table with a predifined keyword

To drop all the tables starting with a certain string .

        declare @cmd varchar(4000)
declare cmds cursor for
Select
    'drop table [' + Table_Name + ']'
From
    INFORMATION_SCHEMA.TABLES
Where
    Table_Name like 'prefix%'

open cmds
while 1=1
begin
    fetch cmds into @cmd
    if @@fetch_status != 0 break
    exec(@cmd)
end
close cmds
deallocate cmds

No comments:

Post a Comment