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;
hoặc có thể thứ:
-- CAUTION
-- Drops all user tables in the current database.
WHILE EXISTS(SELECT [name] FROM sys.tables WHERE [type=mwadah] = 'U')
BEGIN
DECLARE @table_name varchar(50)
DECLARE table_cursor CURSOR FOR SELECT [name] FROM sys.tables WHERE [type=mwadah] = 'U'
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC ('DROP TABLE [' + @table_name + ']')
PRINT 'Dropped Table ' + @table_name
END TRY
BEGIN CATCH END CATCH
FETCH NEXT FROM table_cursor INTO @table_name
END
CLOSE table_cursor
DEALLOCATE table_cursor
END
Nguồn bài viết:
Dngaz.com