: q2 x+ T. o+ W8 G! t, [ q7 [8 @解决方案: 5 i+ {8 |( Z, B2 F create table #temp( IDUnique int Identity ID int, grp int)Insert into #temp(ID) Values(1)Insert into #temp(ID) Values(36)Insert into #temp(ID) Values(37)Insert into #temp(ID) Values(38)Insert into #temp(ID) Values(50)Insert into #temp(ID) Values(51)declare @IDUnique intdeclare @PreviousUnique intdeclare @ID intdeclare @grp intdeclare @Previous intdeclare @Row intDECLARE @getAccountID CURSOR SET @getAccountID = CURSOR FOR SELECT Row_Number() Over(Order by IDUnique) Row,IDUnique,ID From #tempOPEN @getAccountIDFETCH NEXT FROM @getAccountID INTO @Row,@IDUnique,@ID WHILE @@FETCH_STATUS = 0BEGIN IF(@Row = 1) Begin update #temp set grp = 1 Where IDUnique = @IDUnique set @Previous = @ID set @grp = 1 End Else If (@Previous 1 = @ID) Begin update #temp set grp = @grp Where IDUnique = @IDUnique set @Previous = @ID End Else Begin set @Previous = @ID set @grp = @grp update #temp set grp = @grp Where IDUnique = @IDUnique End FETCH NEXT FROM @getAccountID INTO @Row,@IDUnique,@IDENDCLOSE @getAccountIDDEALLOCATE @getAccountIDSelect * from #tempDrop Table #temp