SQLServer2008/2012 刪除全部表視圖存儲過程

SQLServer2008/2012 刪除全部表視圖存儲過程sql

use xuwenbin111
--/第1步**********刪除全部表的外鍵約束*************************/
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1
GOfetch

--/第2步**********刪除全部表*************************/
Go
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin
SELECT @sql='drop table ' + name
FROM sysobjects
WHERE (type = 'U')
ORDER BY 'drop table ' + name
exec(@sql) 
end.net

--/第3步**********刪除全部的視圖*************************/
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='V')>0
begin
SELECT @sql='drop view ' + name
FROM sysobjects
WHERE (type = 'V')
ORDER BY 'drop view ' + name
exec(@sql) 
endget

--/第4步**********刪除全部的存儲過程*************************/
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='P')>0
begin
SELECT @sql='drop proc ' + name
FROM sysobjects
WHERE (type = 'P')
ORDER BY 'drop table ' + name
exec(@sql) 
endtable

相關文章
相關標籤/搜索