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]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM