删除数据库所有table的资料之一

2014-11-24 08:36:14 ? 作者: ? 浏览: 1
--禁所有外 的 句
select 'ALTER TABLE [' + b.name + '] NOCHECK CONSTRAINT [' + a.name +'];'
from sysobjects a ,sysobjects b
where a.xtype ='f' and a.parent_obj = b.id
--or
select 'ALTER TABLE [' + name + '] NOCHECK CONSTRAINT all '
from sysobjects a
where a.xtype ='u'

--禁所有 器的 句
select 'ALTER TABLE [' + name + '] DISABLE TRIGGER all '
from sysobjects a
where a.xtype ='u'




-- 除所有表 料
/*
select 'TRUNCATE TABLE [' + a.name + '] '
from sysobjects a
where a.xtype ='u'
*/
--因 要加GO,在一 句 解 不了。
declare @name varchar(100)
declare cur cursor for
select name from sysobjects where xtype='u' order by name
open cur
fetch next from cur into @name
while @@fetch_status=0
begin

print 'TRUNCATE TABLE [' +@name + '] '
print 'GO'

fetch next from cur into @name
end
close cur
deallocate cur


/*
select 'DELETE [' + a.name + '] '
from sysobjects a
where a.xtype ='u'
*/
-- 是加Go的更好用。中 一 出 了也 有 系
declare @name varchar(100)
declare cur cursor for
select name from sysobjects where xtype='u' order by name
open cur
fetch next from cur into @name
while @@fetch_status=0
begin

print 'DELETE [' +@name + '] '
print 'GO'

fetch next from cur into @name
end
close cur
deallocate cur



-- 所有外 的 句
select 'ALTER TABLE [' + b.name + '] CHECK CONSTRAINT [' + a.name +'];'
from sysobjects a ,sysobjects b
where a.xtype ='f' and a.parent_obj = b.id
or
select 'ALTER TABLE [' + name + '] CHECK CONSTRAINT all '
from sysobjects a
where a.xtype ='u'

---- 所有 器的 句
select 'ALTER TABLE [' + name + '] enable TRIGGER all '
from sysobjects a
where a.xtype ='u'


--所有identity表 原 1的 句
select 'dbcc checkident(['+name+'],reseed ,0) '
from sysobjects a
where a.xtype ='u' and objectproperty(id,'TableHasIdentity')=1


--重建所有索引

select 'dbcc DBREINDEX(['+name+']) '
from sysobjects a
where a.xtype ='u'
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: