Thursday, October 29, 2009

SQL Server, Drop All Tables in Database Quickly

SELECT name INTO #tables from sys.objects where type = 'U'

while (SELECT count(1) FROM #tables)> 0

begin

declare @sql varchar(max)

declare @tbl varchar(255)

SELECT top 1 @tbl = name FROM #tables

SET @sql = ‘drop table [' + @tbl + ']‘

exec(@sql)

DELETE FROM #tables where name = @tbl

end

DROP TABLE #tables;