CREATE TABLE [dbo].[tb] ( [name] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO insert into tb Select ('a') union select ('b') union select ('b') union select ('a') union select ('a') union select ('b') declare @i int declare @y varchar(100) select identity(int,1,1) id,0as uid ,name into # from tb order by name Update # Set uid = Case When name = @y then @i else1 end, @i =case When name = @y Then @i +1else1 end, @y = name id uid name ------------------------------------------ 11 a 22 a 33 a 41 b 52 b 63 b