清空数据库记录

http://www.cnblogs.com/kingkoo/archive/2008/02/24/1079157.html

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)

 

该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表

declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where name like 'T_%' and type='U'
--select 'truncate table ' + name from sysobjects where name like 'T_%' 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 ?"

 

你可能感兴趣的:(数据库)