清空數(shù)據(jù)庫(kù)中所有表記錄 記錄ID恢復(fù)從0開始
更新時(shí)間:2010年08月26日 01:11:39 作者:
近來發(fā)現(xiàn)數(shù)據(jù)庫(kù)過大,空間不足,因此打算將數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行全面的清理,但表非常多,一張一張的清空,實(shí)在麻煩,因此就想利用SQL語句一次清空所有數(shù)據(jù).找到了三種方法進(jìn)行清空.使用的數(shù)據(jù)庫(kù)為MS SQL SERVER.
1.搜索出所有表名,構(gòu)造為一條SQL語句
declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
該方法適合表不是非常多的情況,否則表數(shù)量過多,超過字符串的長(zhǎng)度,不能進(jìn)行完全清理.
2.利用游標(biāo)清理所有表
declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
這是我自己構(gòu)造的,可以做為存儲(chǔ)過程調(diào)用, 能夠一次清空所有表的數(shù)據(jù),并且還可以進(jìn)行有選擇的清空表.
3.利用微軟未公開的存儲(chǔ)過程
exec sp_msforeachtable "truncate table ?"
該方法可以一次清空所有表,但不能加過濾條件.
復(fù)制代碼 代碼如下:
declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
該方法適合表不是非常多的情況,否則表數(shù)量過多,超過字符串的長(zhǎng)度,不能進(jìn)行完全清理.
2.利用游標(biāo)清理所有表
復(fù)制代碼 代碼如下:
declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
這是我自己構(gòu)造的,可以做為存儲(chǔ)過程調(diào)用, 能夠一次清空所有表的數(shù)據(jù),并且還可以進(jìn)行有選擇的清空表.
3.利用微軟未公開的存儲(chǔ)過程
復(fù)制代碼 代碼如下:
exec sp_msforeachtable "truncate table ?"
該方法可以一次清空所有表,但不能加過濾條件.
相關(guān)文章
sql server 中合并某個(gè)字段值的實(shí)例
sql server 中合并某個(gè)字段值的實(shí)例,需要的朋友可以參考一下2013-03-03sql server 2000數(shù)據(jù)庫(kù)備份還原的圖文教程
MSSQL是微軟公司的一款數(shù)據(jù)庫(kù)管理系統(tǒng),本文將詳細(xì)介紹MSSQL2000中數(shù)據(jù)庫(kù)的備份和還原功能,需要的朋友可以參考下2014-08-08SQL語句 操作全集 學(xué)習(xí)mssql的朋友一定要看
SQL操作全集 下列語句部分是Mssql語句,不可以在access中使用。2009-03-03vs code連接sql server數(shù)據(jù)庫(kù)步驟及遇到的問題小結(jié)
這篇文章主要介紹了用vs code連接sql server數(shù)據(jù)庫(kù)步驟及遇到的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05SqlServer高版本數(shù)據(jù)備份還原到低版本
這篇文章主要為大家詳細(xì)介紹了SqlServer高版本數(shù)據(jù)備份還原到低版本的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11SQLServer中字符串左對(duì)齊或右對(duì)齊顯示的sql語句
在顯示數(shù)據(jù)時(shí)需要對(duì)數(shù)據(jù)進(jìn)行美觀化顯示。如左對(duì)齊,右對(duì)齊2012-05-05詳解SQL Server數(shù)據(jù)庫(kù)鏈接查詢的方式
本文我們主要介紹了SQL Server數(shù)據(jù)庫(kù)鏈接查詢的方式,包括內(nèi)連接、外連接和交叉連接等的內(nèi)容,需要的朋友可以參考下2015-08-08SQL入侵恢復(fù)xp_cmdshell方法總結(jié)
恢復(fù)xp_cmdshell SQL Server阻止了對(duì)組件 'xp_cmdshell' 的過程'sys.xp_cmdshell' 啟用2010-08-08