當我們需要批量刪除數據庫中的表時,對於單個刪除一些表是否感到煩躁,厭倦,干脆寫個腳本用得了。
本腳本使用游標循環刪除,對於數量比較小,用游標暫不會造成惡劣影響。
復制代碼 代碼如下:
DECLARE @tablename VARCHAR(30),
@sql VARCHAR(500)
DECLARE cur_delete_table CURSOR READ_ONLY FORWARD_ONLY FOR
SELECT name FROM sysobjects WHERE name LIKE 'PUB%' AND type='U'
OPEN cur_delete_table
FETCH NEXT FROM cur_delete_table INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql='drop table '+@tablename
EXEC (@sql)
FETCH NEXT FROM cur_delete_table INTO @tablename
END
CLOSE cur_delete_table
DEALLOCATE cur_delete_table