查找SQL Server 自增ID值不连续记录
在很多的时候,我们会在数据库的表中设置一个字段:ID,这个ID是一个IDENTITY,也就是说这是一个自增ID。当并发量很大并且这个字段不是主键的时候,就有可能会让这个值重复;或者在某些情况(例如插入数据的时候出错,或者是用户使用了Delete删除了记录)下会让ID值不是连续的,比如1,2,3,5,6,7,10,那么在中间就断了几个数据,那么我们希望能在数据中找出这些相关的记录,我希望找出的记录是3,5,7,10,通过这些记录可以查看这些记录的规律来分析或者统计;又或者我需要知道那些ID值是没有的:4,8,9。
解决办法的核心思想是: 获取到当前记录的下一条记录的ID值,再判断这两个ID值是否差值为1,如果不为1那就表示数据不连续了
执行下面的语句生成测试表和测试记录
--生成测试数据
if exists (select * from sysobjects where id = OBJECT_ID('[t_IDNotContinuous]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [t_IDNotContinuous] CREATE TABLE [t_IDNotContinuous] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[ValuesString] [nchar] (10) NULL) SET IDENTITY_INSERT [t_IDNotContinuous] ON INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 1,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 2,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 3,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 5,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 6,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 7,'test')
INSERT [t_IDNotContinuous] ([ID],[ValuesString]) VALUES ( 10,'test') SET IDENTITY_INSERT [t_IDNotContinuous] OFF select * from [t_IDNotContinuous]

(图1:测试表)
--拿到当前记录的下一个记录进行连接
select ID,new_ID
into [t_IDNotContinuous_temp]
from (
select ID,new_ID = (
select top 1 ID from [t_IDNotContinuous]
where ID=(select min(ID) from [t_IDNotContinuous] where ID>a.ID)
)
from [t_IDNotContinuous] as a
) as b select * from [t_IDNotContinuous_temp]

(图2:错位记录)
--不连续的前前后后记录
select *
from [t_IDNotContinuous_temp]
where ID <> new_ID - 1 --查询原始记录
select a.* from [t_IDNotContinuous] as a
inner join (select *
from [t_IDNotContinuous_temp]
where ID <> new_ID - 1) as b
on a.ID >= b.ID and a.ID <=b.new_ID
order by a.ID

(图3:效果)
补充1:如果这个ID字段不是主键,那么就会有ID值重复的情况(有可能是一些误操作,之前就有遇到过)那么就需要top 1来处理。但是当前这种情况下可以使用下面的简化语句
select a.id as oid, nid =
(select min(id) from t_IDNotContinuous b where b.id > a.id)
from t_IDNotContinuous a
补充2:缺失ID值列表,
--方法一:找出上一条记录+1,再比较大小
select (select max(id)+1
from [t_IDNotContinuous]
where id<a.id) as beginId,
(id-1) as endId
from [t_IDNotContinuous] a
where
a.id>(select max(id)+1 from [t_IDNotContinuous] where id<a.id)

(图4:效果)
--方法二:全部+1,再判断在原来记录中找不到
select beginId,
(select min(id)-1 from [t_IDNotContinuous] where id > beginId) as endId
from (
select id+1 as beginId from [t_IDNotContinuous]
where id+1 not in
(select id from [t_IDNotContinuous])
and id < (select max(id) from [t_IDNotContinuous])
) as t
查找SQL Server 自增ID值不连续记录的更多相关文章
- 简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录
原文:简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录 在很多的时候,我们会在数据库的表中设置一个字段:ID,这个ID是一个IDENTITY,也就是说这是一个自增ID.当并发 ...
- Sql Server自增ID与序号的使用
SQL 自增ID alter table a add id int identity(1,1) not null 这里为 a 表增加一个 id 字段,其中identity(1,1)代表自增,第一个1代 ...
- sql server自增列值的获取
IDENT_CURRENT(tbname) 是看表对象.所以没有受作用域限制. SCOPE_IDENTITY() 受作用域限制.同一个会话里面不同作用域也会有差异 @@IDENTITY 受会话限制 ...
- sql server 自增列,值突然增大1000的情况
sql server 自增列,值突然增大1000的情况 解决方法: 1 打开配置管理器2左面点击sql服务3右面 右键点击SQL Server(MSSQLSERVER) 4点击 启动参数5 在参数 ...
- 使用java对sql server进行增删改查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- sql server like 在将值转换成数据类型int失败
select * from table where title like '%'?'%'; 采用? 传参会报错:sql server like 在将值转换成数据类型int失败 select * fro ...
- Windows Server 2012搭建SQL Server Always On踩坑全记录
Windows Server 2012搭建SQL Server Always On踩坑全记录 环境信息: Windows Server 2012 R2 Sql Server 2012 整个搭建集群的过 ...
- Sql server 中关闭ID自增字段(SQL取消ID自动增长)
sql server在导入数据的时候,有时候要考虑id不变,就要先取消自动增长再导入数据,导完后恢复自增. 比如网站改版从旧数据库导入新数据库,数据库结构不相同,可能会使用insert into xx ...
- SQL Server数据库sql语句生成器(SqlDataToScript)的使用(sql server自增列(id)插入固定值)
SqlDataToScript是根据表数据进行生成 Insert Into语句,此工具还有一个好处是可以对自增列插入固定值,例如:自增的列id值为5,但是5这个行值已经删除,如果想存储Id自增列值为5 ...
随机推荐
- [LeetCode] 628. Maximum Product of Three Numbers_Easy
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- http接口自动化测试框架实现
一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值 输出:XML文件 eg:http://xxx.com/xxx_product/test/conten ...
- vue项目优化
cross-env 包环境 静态文件分离 require 是置顶的 双斜杠 //baidu.com可以是http也可以是https require.ensure打包到不同的文件中 项目文件路径规范 ...
- Android下基于线程池的网络访问基础框架
引言 现在的Android开发很多都使用Volley.OkHttp.Retrofit等框架,这些框架固然有优秀的地方(以后会写代码学习分享),但是我们今天介绍一种基于Java线程池的网络访问框架. 实 ...
- JSP—cookie
cookie的作用: 1.对特定对象的追踪,如访问次数,最后访问时间,路径等 2.统计网页的浏览次数 3.在cookie有效期内,记录用户的登录信息 4.实现个性化,记录用户的喜好 5.保存的数据存在 ...
- <Convolutional Neural Network for Paraphrase Identification>
code:https://github.com/chantera/bicnn-mi Yin的这篇论文提出了一种叫Bi-CNN-MI的架构,其中Bi-CNN表示两个使用Siamese框架的CNN模型:M ...
- Linux命令:查看文件内容cat|tac|more|less|head|tail|nl|od
查看文件内容的命令;cat, tac, more, less, head, tail, nl, 1)cat 由第一行开始显示文档内容,一直显示到最后 2)tac 从最后一行开始显示,一直显示到第一行内 ...
- Nginx启动SSL功能
Nginx启动SSL功能,并进行功能优化,你看这个就足够了 一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 nginx: [emerg] the &q ...
- python之路----进程(一)
一.理论知识1.操作系统发展简介 1.没有操作系统 —— 穿孔卡片 2.批处理系统 —— 串行 ,速度块 联机批处理 读磁带的时候速度快 脱机批处理 读磁带和cpu工作并发 3.多道程序系统 —— 并 ...
- 计算概论(A)/基础编程练习1(8题)/5:鸡兔同笼
#include<stdio.h> int main() { // 鸡兔同笼中脚的总数:a < 32768 int a; scanf("%d", &a); ...