清空數(shù)據(jù)庫中所有表記錄 記錄ID恢復從0開始
發(fā)布日期:2022-01-28 14:43 | 文章來源:站長之家
復制代碼 代碼如下:
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ù)量過多,超過字符串的長度,不能進行完全清理.
2.利用游標清理所有表
復制代碼 代碼如下:
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
這是我自己構造的,可以做為存儲過程調(diào)用, 能夠一次清空所有表的數(shù)據(jù),并且還可以進行有選擇的清空表.
3.利用微軟未公開的存儲過程
復制代碼 代碼如下:
exec sp_msforeachtable "truncate table ?"
該方法可以一次清空所有表,但不能加過濾條件.
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
關注官方微信