查询时生成一个字段,并自动加1.
- SQL code
表Anameabcdeselect name,aa from a希望结果: name aa a 1 b 2 c 3 d 4 e 5
请教了.....
[解决办法]
select a.name,(select count(*)+1 from a b where b.name < a.name) aa from a
[解决办法]
有主键的可以直接用:
select 自增列=(select sum(1) from 表 where 主键<=a.主键),* from 表 a
没有主键的需要用临时表:
select 自增列=identity(int,1,1),* into #tb from 表
select * from #tb
drop table #tb
- SQL code
select nField=(select sum(1) from p1 where sid<=a.sid),* from tb aselect nField=identity(int,1,1),* into ##tb from tbselect * from ##tbdrop table ##tb
[解决办法]
呵呵,楼上很多高手!
不过需要说明一下,自增字段(identity(1,1))要在临时表里建立,而不能在SELECT语句里建立!