1.搜索出所有表名,构造为一条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)
set @trun_name=”
select @trun_name=@trun_name + ‘truncate table ‘ + [name] + ‘ ‘ from sysobjects where xtype=’u’ and status > 0
exec (@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
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
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
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
3.利用微软未公开的存储过程
复制代码 代码如下:
exec sp_msforeachtable “truncate table ?”
该方法可以一次清空所有表,但不能加过滤条件.
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/618696.html