Created
April 6, 2012 13:11
-
-
Save nkostic/2319648 to your computer and use it in GitHub Desktop.
Drop All Tables in Database MSSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use database_name | |
declare @i int | |
set @i = 0 | |
while (@i<99) | |
begin | |
declare @table_name varchar(100) | |
declare table_list cursor for | |
select name from sysobjects o2 where xtype='U' and | |
not exists ( | |
select * from sysforeignkeys k | |
join syscolumns c1 on (k.fkeyid = c1.id and c1.colid=k.fkey) | |
join syscolumns c2 on (k.rkeyid = c2.id and c2.colid=k.rkey) | |
where c2.id = o2.id and c1.id <> o2.id | |
) | |
open table_list | |
fetch next from table_list into @table_name | |
while @@fetch_status = 0 | |
begin | |
print 'dropping table '+@table_name | |
exec ('drop table '+@table_name) | |
fetch next from table_list into @table_name | |
end | |
close table_list | |
deallocate table_list | |
set @i = @i+1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment