sqlserver删除所有表

 1 --/第1步**********删除所有表的外键约束*************************/
 2  
 3 DECLARE c1 cursor for
 4 select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
 5 from sysobjects
 6 where xtype = 'F'
 7 open c1
 8 declare @c1 varchar(8000)
 9 fetch next from c1 into @c1
10 while(@@fetch_status=0)
11 begin
12 exec(@c1)
13 fetch next from c1 into @c1
14 end
15 close c1
16 deallocate c1
17  
18 --/第2步**********删除所有表*************************/
19  
20 use 命名空间21 GO
22 declare @sql varchar(8000)
23 while (select count(*) from sysobjects where type='U')>0
24 begin
25 SELECT @sql='drop table ' + name
26 FROM sysobjects
27 WHERE (type = 'U')
28 ORDER BY 'drop table ' + name
29 exec(@sql)
30 end

如果存在schema修改的情况,一定要加[schema]

猜你喜欢

转载自www.cnblogs.com/real9527/p/10568874.html