您现在的位置是:网站首页> 编程资料编程资料
清空数据库中所有表记录 记录ID恢复从0开始_MsSql_
2023-05-26
366人已围观
简介 清空数据库中所有表记录 记录ID恢复从0开始_MsSql_
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 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 ?"
该方法可以一次清空所有表,但不能加过滤条件.
复制代码 代码如下:
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 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 ?"
该方法可以一次清空所有表,但不能加过滤条件.
相关内容
- sqlserver中向表中插入多行数据的insert语句_MsSql_
- sqlserver 聚集索引和非聚集索引实例_MsSql_
- ADO.NET EF中的实体修改方法_MsSql_
- sqlserver 函数、存储过程、游标与事务模板_MsSql_
- SQL入侵恢复xp_cmdshell方法总结_MsSql_
- 世界杯猜想活动的各类榜单的SQL语句小结_MsSql_
- SQLServer 2000 数据库同步详细步骤[两台服务器]_MsSql_
- 几个扩展存储过程使用方法_MsSql_
- 分发服务器 系统抛出18483错误,未能连接服务器,因为'distributor_admin'未定义远程登陆_MsSql_
- 错误22022 SQLServerAgent当前未运行的解决方法_MsSql_
