/******執(zhí)行此操作時請慎重,確認無誤后把事務(wù)去掉既可******/
BEGIN TRAN --事務(wù) 開始
select * from sysobjects where (type='U' or type='P' or type='FN' or type='TR') and category<>2
declare @StrName nvarchar(200)
--刪除 函數(shù)
declare cursorFN cursor for
select name from sysobjects where type='FN'
open cursorFN
while 1=1
begin
fetch next from cursorFN into @StrName
if @@fetch_status<>0 break
exec('DROP FUNCTION '+@StrName)
end
close cursorFN
deallocate cursorFN
--刪除 存儲過程
declare cursorP2 cursor for
select name from sysobjects where type='P' and category<>2
open cursorP2
while 1=1
begin
fetch next from cursorP2 into @StrName
if @@fetch_status<>0 break
exec('DROP PROCEDURE '+@StrName)
end
close cursorP2
deallocate cursorP2
--刪除 表 (刪時自動刪除觸發(fā)器)
--1刪無約束無外鍵的表
declare cursorU1 cursor for
select a.name from sysobjects a where not exists(select 1 from sysobjects b where a.id=b.parent_obj) and a.type='U'
open cursorU1
while 1=1
begin
fetch next from cursorU1 into @StrName
if @@fetch_status<>0 break
exec('DROP TABLE '+@StrName)
end
close cursorU1
deallocate cursorU1
--2刪有約束或有外鍵的表
while exists(select 1 from sysconstraints)
begin
declare cursorU2 cursor for
select c.name from sysobjects c join (select distinct id from sysconstraints a
where not exists(select 1 from sysforeignkeys b where a.id=b.rkeyid)) d on c.id=d.id where c.type='U'
open cursorU2
while 1=1
begin
fetch next from cursorU2 into @StrName
if @@fetch_status<>0 break
exec('DROP TABLE '+@StrName)
end
close cursorU2
deallocate cursorU2
end
select * from sysobjects where (type='U' or type='P' or type='FN' or type='TR') and category<>2
ROLLBACK TRANSACTION --事務(wù) 回滾