共計 896 個字符,預(yù)計需要花費 3 分鐘才能閱讀完成。
這篇文章主要講解了“怎么清空數(shù)據(jù)庫中所有表記錄”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么清空數(shù)據(jù)庫中所有表記錄”吧!
1. 搜索出所有表名, 構(gòu)造為一條 SQL 語句復(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ù)量過多, 超過字符串的長度, 不能進行完全清理. 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)造的, 可以做為存儲過程調(diào)用, 能夠一次清空所有表的數(shù)據(jù), 并且還可以進行有選擇的清空表. 3. 利用微軟未公開的存儲過程復(fù)制代碼 代碼如下:
exec sp_msforeachtable truncate table ?
該方法可以一次清空所有表, 但不能加過濾條件.
感謝各位的閱讀,以上就是“怎么清空數(shù)據(jù)庫中所有表記錄”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么清空數(shù)據(jù)庫中所有表記錄這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!