删除数据库所有table的资料之一
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 12:34 点击:次
--禁所有外鍵的語句
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+']) '&nbs
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+']) '&nbs
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>