清除數據庫 所有表 視圖 存儲過程 觸發器

Begin Transaction 
Begin try
-- Drop Foreign Key 
declare @SQL varchar(max),
  @table varchar(50),
  @Constraint varchar(50)
Declare Mycursor cursor for select object_name(parent_obj),Name from sysobjects where xtype='F'  
open mycursor 
fetch next from mycursor into @table,@Constraint
while @@FETCH_STATUS=0
Begin
 set @SQL='alter table ['+@table +'] drop constraint ['+@Constraint+']'
 exec(@SQL)
 Fetch next from mycursor into @table,@Constraint
End
Close Mycursor 
Deallocate Mycursor 
--drop table 
set @SQL=''
select @SQL=@SQL+name+',' from sysobjects where xtype='U' and name<>'dtproperties'
If ISNULL(@sql,'')!=''
Begin
 set @SQL='Drop table '+LEFT(@sql,len(@sql)-1)
 exec(@SQL)
End 
--drop Proc
set @SQL=''
select @SQL=@SQL+name+',' from sysobjects where xtype='P' and name<>'DropDatabase'
If ISNULL(@sql,'')!=''
Begin
 set @SQL='Drop Proc '+LEFT(@sql,len(@sql)-1)
 exec(@SQL)
End
--drop View
set @SQL=''
select @SQL=@SQL+name+',' from sysobjects where xtype='V' and name<>'DropDatabase'
If ISNULL(@sql,'')!=''
Begin
 set @SQL='Drop View '+LEFT(@sql,len(@sql)-1)
 exec(@SQL)
End
 set @SQL=''
select @SQL=@SQL+name+',' from sysobjects where xtype='TR' and name<>'DropDatabase'
If ISNULL(@sql,'')!=''
Begin
 set @SQL='Drop Trigger '+LEFT(@sql,len(@sql)-1)
 exec(@SQL)
End
commit Transaction
End Try 
Begin Catch
rollback tran
declare @message varchar(1000)
set @message='Error_Number:'+ERROR_NUMBER()+CHAR(13)+
'Error_Line  :'+ERROR_LINE()+CHAR(13)+
'Error_message:'+ERROR_MESSAGE()
RaisError(@message,16,1)
End Catch 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章