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]sql