SqlServer连号检测

1、建立测试数据

SqlServer连号检测
if exists (select * from sysobjects where id = OBJECT_ID('tb') and OBJECTPROPERTY(id, 'IsUserTable') = 1) 

DROP TABLE tb

 

CREATE TABLE tb (

 [ID] int  IDENTITY (1, 1)  NOT NULL,

 [ValuesString] nchar  (10) NULL)

 

SET IDENTITY_INSERT tb ON

 

INSERT tb ([ID],[ValuesString]) VALUES ( 1,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 2,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 3,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 5,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 6,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 7,'test')

 INSERT tb ([ID],[ValuesString]) VALUES ( 10,'test')

 

SET IDENTITY_INSERT tb OFF

 

select * from tb

 
View Code

2、查询

SELECT  id ,

        new_id = ( SELECT   MIN(ID)

                   FROM     tb

                   WHERE    ID > a.ID

                 )

FROM    TB AS a

3、查询

SELECT  id ,

        new_id = ( SELECT   MIN(ID)

                   FROM     tb

                   WHERE    ID > a.ID

                 )

FROM    TB AS a

WHERE id<>( SELECT   MIN(ID)-1

                   FROM     tb

                   WHERE    ID > a.ID

                 )

 

你可能感兴趣的:(sqlserver)