--测试数据
declare @t table(Name varchar(10),Age int,Sex varchar(2))
insert into @t
select '张三',10,'男'
union all select '李四',12,'男'
union all select '王五',14,'女'
union all select '老六',45,'男'
--查询全部
select id=(select sum(1) from @t where name<=a.name),*
from @t a order by name
--查询部分
select id=(select sum(1) from @t where name<=a.name and age>12),*
from @t a
where age>12
order by name
/*--测试结果
id Name Age Sex
----------- ---------- ----------- ----
1 老六 45 男
2 李四 12 男
3 王五 14 女
4 张三 10 男
(所影响的行数为 4 行)
id Name Age Sex
----------- ---------- ----------- ----
1 老六 45 男
2 王五 14 女