我打算发了一些连续号段的卡片到数据库中,我怎么判断这些号段确实是连续的
因为我们发卡的时候,有时候写卡失败了,那这个号码就不能写到数据库里了。今天发了2000张卡,最后我想知道卡号是在哪些地方是间断的,然后我再补充,怎么写语句查呢,可以查吗?号段是int类型 数据库 查询?
[解决办法]
用row_number排个序,用你的编号减去这个排序段,找出不一样的数,找出每个最小的,就是断的..
[解决办法]
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]
go
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]
go
select id
from [t_IDNotContinuous_temp]
where ID <> new_ID - 1