select查询数据时,同时添加自增里的方法

转载:http://www.maomao365.com/?p=10197

create table [maomao365.com]
(info varchar(20))
go

insert [maomao365.com]
(info)values('sqlserver'),
(N'猫猫小屋'),(N'sql爱好者')
go

  --方法1:使用row_number生成自编号
  select ROW_NUMBER() over(order by info asc ) as [自编号],
  info from [maomao365.com]

  --方法2: 使用identity(1,1)生成自编号
  select identity(int,1,1) as [自编号]
  ,info  into #t  from [maomao365.com] 
  select * from #t 
   order by info asc 



go
truncate table [maomao365.com]
drop     table [maomao365.com]

 

你可能感兴趣的:(sqlserver,2008,SQLSERVER)