删除某数据库下的所有表

来源:互联网 发布:模因 知乎 编辑:程序博客网 时间:2024/05/16 01:12

应用场景:升级ACCESS数据库到MSSQL数据库,ID自增需要调整为跟ACCESS数据库中的自增字段一样的编号,导致需要不断的添加记录跟删除表重新测试.

技术要点:SQL游标和变量使用。

注意场合:请注意删除前一定要做数据库备份,删除数据后会导致数据库中所有的数据表和表中的记录数据丢失,没备份千万不要尝试,删除数据的后果请读者自负。

操作步骤:请运行脚本前一定要选中要删除的数据库,然后执行下面代码 中的脚本

代码如下:


declare @CurrentTableName nvarchar(100)
declare @CurrentTableObjectID int
declare @deletetableSqlString nvarchar(1000)
--select * from Sys.all_objects where type='U' ;
declare tb cursor local for select name,object_id from Sys.all_objects where type='U' ;
open tb 
fetch next from tb into @CurrentTableName,@CurrentTableObjectID
while @@fetch_status=0
begin
set @deletetableSqlString='drop table '+@CurrentTableName
exec sp_executesql @deletetableSqlString;
print '删除数据表'+@CurrentTableName +'完成'
fetch next from tb into @CurrentTableName,@CurrentTableObjectID
end
close tb
deallocate tb

0 0
原创粉丝点击