SQL Server 数据库维护脚本合集[005]-删除数据库所有用户表

--*****************************************************************************
-- @作    者:  qwu;
-- @用    途:  删除数据库所有用户表;
-- @版    本:  v1.0
-- @创建日期:  2011-3-23;
-- @用    法:  设置use [数据库名],
-- @注    意:  !!!!!!!!慎用!!!!!!!!!;
-- @修改历史:  
--   2011-3-23 qwu 创建;
--*****************************************************************************

--!!!!!!!!!!!!!!!!!!!!!
--!!!!!!!!慎用!!!!!!!!!
--!!!!!!!!!!!!!!!!!!!!!

--BEGIN--
BEGIN TRAN

use [数据库名]

--删除外键约束
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

--删除所有用户表
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

ROLLBACK
--END--

posted @ 2011-03-28 15:26  小肥侠  阅读(343)  评论(0编辑  收藏  举报