SQL Server删除所有表的小脚本

最近在测试东西,这段话很常用,帖在这里方便copy

思路就是先干掉约束再删表

--删除外键约束 DECLARE c1 cursor for select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; ' from sysobjects where xtype = 'F' open c1 declare @c1 varchar(8000) fetch next from c1 into @c1 while(@@fetch_status=0) begin exec(@c1) fetch next from c1 into @c1 end close c1 deallocate c1 --删除表 DECLARE c2 cursor for select 'drop table ['+name +']; ' from sysobjects where xtype = 'u' open c2 declare @c2 varchar(8000) fetch next from c2 into @c2 while(@@fetch_status=0) begin exec(@c2) fetch next from c2 into @c2 end close c2 deallocate c2 exec sp_msforeachtable @command1="truncate table ? ;" 

你可能感兴趣的:(SQL Server删除所有表的小脚本)