您的位置:首页 > 数据库

使用SQL语句清空数据库所有表的数据

2015-03-03 09:50 519 查看
近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL SERVER.
1.搜索出所有表名,构造为一条SQL语句

650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />declare @trun_name varchar(8000)
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />set @trun_name=''
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />exec (@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />declare @trun_name varchar(50)
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />declare name_cursor cursor for
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />open name_cursor
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />fetch next from name_cursor into @trun_name
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />while @@FETCH_STATUS = 0
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />begin
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />exec (@trun_name)
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />print 'truncated table ' + @trun_name
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />fetch next from name_cursor into @trun_name
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />end
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />close name_cursor
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />deallocate name_cursor
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
3.利用微软未公开的存储过程
650) this.width=650;" alt="" src="http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1" align="top" />exec sp_msforeachtable "truncate table ?"
该方法可以一次清空所有表,但不能加过滤条件.

本文出自 “ghost” 博客,请务必保留此出处http://caizi.blog.51cto.com/5234706/1565875
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: