当我们需要批量删除数据库中的表时,对于单个删除一些表是否感到烦躁,厌倦,干脆写个脚本用得了。
本脚本使用游标循环删除,对于数量比较小,用游标暂不会造成恶劣影响。
代码如下:
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
原文链接:https://www.f2er.com/mssql/63338.html@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