sql语句解决自增关键字重排/初始化问题
2010-08-26 16:37
344 查看
来自:http://hi.baidu.com/ibelieve9/blog/item/686a34a4ebec54f29052ee91.html 将Customer替换为自己的表 ID为自增字段 使用SQL Server的自增功能来管理表的关键字,时间久后由于删除原因ID会不连续,如何重新“整理”关键字ID,使其重新从1开始,并且重置自增初始值为当前记录个数? /*允许对系统表进行更新*/ exec sp_configure 'allow updates',1 reconfigure with override GO /*取消标识列标记 */ update syscolumns set colstat = 0 where id = object_id('dbo.Customer') and colstat = 1 GO /*所有对记录的ID进行重排*/ update dbo.Customer set ID=(select count(1) from dbo.Customer where ID<=t.ID) from dbo.Customer t GO /*得到重排后的记录总个数*/ declare @a int set @a=(select count(*) from dbo.Customer) /*重新设置标识的起始值*/ DBCC CHECKIDENT (Customer, RESEED, @a) GO /*恢复标识列标记*/ update syscolumns set colstat = 1 where id = object_id('dbo.Customer') and name = 'ID' GO /*禁止对系统表进行更新*/ exec sp_configure 'allow updates',0 reconfigure with override |
相关文章推荐
- 解决 JMeter 中 SQL 语句参数化的问题
- Mysql中文乱码以及导出为sql语句和Excel问题解决
- 解决“每个Y的最新X”问题的SQL语句
- 解决ssm环境下配置log4j打印mybatis的sql语句无效的问题
- 性能优化分析案例---解决SQL语句过度消耗CPU问题
- Android中的SQL查询语句LIKE绑定参数问题解决办法(sqlite数据库)
- 如何解决在.Net中用Sql语句向SqlServer数据库中插入特殊字符失败的问题?
- 解决 JMeter 中 SQL 语句参数化的问题
- C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?
- 解决SQL语句过度消耗CPU问题
- Node.js 回调函数来解决SQL语句与返回值的异步问题
- mysql点滴_02程序中执行sql语句报字符集问题解决
- 关于mysql里面写sql语句,或者直接在表里写汉字,都是乱码问题解决
- 执行SQL语句时出现问题操作必须使用一个可更新的查询错误的解决方法
- Oracle执行计划 SQL语句执行效率问题查找与解决方法
- DataTable实现列位置交换,用于SQL语句无法解决字段页面显示顺序问题
- C# SQL语句执行时间过长在操作完成之前超时时间已过或服务器未响应问题的解决
- 用SQL语句解决mysql导入大数据文件的问题
- Hibernate中多表联合查询遇到的问题(原生态的SQL语句解决办法)
- 使用 Navicat for MySQL 执行 SQL 语句时,出现 out of memory 问题的解决方法