首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

小弟我想给查询语句返回的记录加个自动编号列

2012-03-28 
我想给查询语句返回的记录加个自动编号列。比如我这样一个语句:selectname,age,sexfromstudent返回结果是:n

我想给查询语句返回的记录加个自动编号列。
比如我这样一个语句:select   name,age,sex   from   student
返回结果是:
name     age     sex
xx         20         男
yy         21         女
zz         22         男
我想返回结果变成:
bh           name         age         sex
第1个       xx             20           男
第2个       yy             21           男
第3个       xx             22           男

这个bh列就是根据返回记录的行数而变的。。
请问这个该怎么做?

[解决办法]
select bh = identity(int,1,1) , name,age,sex into @temp from student

select '第 ' + cast(bh as varchar) + '个 ' , name,age,sex from @temp

[解决办法]
如果根据name区分大小,可不用临时表.

select '第 ' + cast(bh as varchar) + '个 ' bh, name,age,sex from
(
SELECT * , bh=(SELECT COUNT(name) FROM student WHERE name > a.name) + 1 FROM student a
) t
order by bh

热点排行