一次性刪除數據庫裏的所有表

--/第1步***********刪除所有外鍵約束**************/

  1. DECLARE c1 cursor for   
  2.     select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; ' 
  3.     from sysobjects   
  4.     where xtype = 'F' 
  5. open c1  
  6. declare @c1 varchar(8000)  
  7. fetch next from c1 into @c1  
  8. while(@@fetch_status=0)  
  9.     begin   
  10.         exec(@c1)  
  11.         fetch next from c1 into @c1  
  12.     end 
  13. close c1  
  14. deallocate c1  
  15.  

--/第2步**********刪除所有表*************************/

  1. use MedInfoWeb  
  2. declare @tname varchar(8000)  
  3. set @tname='' 
  4. select @tname=@tname + Name + ',' from sysobjects where xtype='U' 
  5. select @tname='drop table ' + left(@tname,len(@tname)-1)  
  6. exec(@tname)  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章