1.利用游标清理所有表
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @trun_name varchar(50)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
declare name_cursor cursor for
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
open name_cursor
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch next from name_cursor into @trun_name
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
while @@FETCH_STATUS = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@trun_name)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
print 'truncated table ' + @trun_name
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch next from name_cursor into @trun_name
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
close name_cursor
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
deallocate name_cursor
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
2.利用微软未公开的存储过程
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
exec sp_msforeachtable "truncate table ?"