sql server 中如何获取表的当前行所在的行数,谢谢

bitshengwuyixue 2009-07-28 10:55:46
想把一个表的数据查询出来, 把每行所在的行数在查询结果中也显示出来,怎么写select语句,谢谢!!
...全文
3118 32 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
c24dnchf 2011-09-21
  • 打赏
  • 举报
回复
sqlserver完全是一个狗屁都不是,又有许多狗屁东西在里面的数据库,微软的三流技术根本不适合开发数据库。
zjybushiren88888 2009-07-28
  • 打赏
  • 举报
回复
基于SQL2005前使用如下


select id,(select count(*) from tb as S2 where S2.id<=S1.id)as
rownum from tb as S1 order by id
id rownum
--------------
1 1
3 2
5 3
8 4
10 5
11 6

zjybushiren88888 2009-07-28
  • 打赏
  • 举报
回复

if object_id('tb') is not null
drop table tb
create table tb
(
id int primary key
)

insert into tb
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11


select id,row_number() over(order by id) as rownum from tb

(6 行受影响)
id rownum
--------------
1 1
3 2
5 3
8 4
10 5
11 6
bitshengwuyixue 2009-07-28
  • 打赏
  • 举报
回复
谢谢楼上的,我先去吃饭了,完了之后我试试,谢谢
feixianxxx 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bitshengwuyixue 的回复:]
假设表a 中有两列数据 name 和 age
其中 name      age
    zhang      16
    li        17
    meng      19
    zhu        20
要求查询结果是
xh  name      age
1    zhang      16
2    li        17
3    meng      19
4    zhu        20
[/Quote]


if object_id('test') is not null 
drop table test
create table test
(
id int primary key
)

insert into test
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11

select *,IDENTity(int,1,1) as xh into ## from test
select * from ##

(6 行受影响)
id xh
----------- -----------
1 1
3 2
5 3
8 4
10 5
11 6


Jamin_Liu 2009-07-28
  • 打赏
  • 举报
回复
declare @a table
(
id int
,shop varchar(10)
,[desc] varchar(50)
,ndqty int
);
insert into @a
values (1,'a','aa',10)
,(2,'b','bb',12)
,(3,'c','cc',15)
,(4,'a','aa',20);

select ROW_NUMBER() over (order by id desc) as rowid
,*
from @a
feixianxxx 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bitshengwuyixue 的回复:]
我是随便写的一个表,用row_number() 在SQL2005种提示是不可识别的函数名
[/Quote]
可以识别
lidanzi 2009-07-28
  • 打赏
  • 举报
回复
[Quote=SQL]

declare @ta table(id varchar(100),info varchar(100))
insert into @ta
select '1001','a' union all
select '1002','b' union all
select '1003','c' union all
select '1004','d'

select row_number() over(order by id)as rownumber,* from @ta
--查询结果--
rownumber id info
1 1001 a
2 1002 b
3 1003 c
4 1004 d
[/Quote]
MSSQLSERVER2005可以
bitshengwuyixue 2009-07-28
  • 打赏
  • 举报
回复
我是随便写的一个表,用row_number() 在SQL2005种提示是不可识别的函数名
yygyogfny 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bitshengwuyixue 的回复:]
假设表a 中有两列数据 name 和 age
其中 name      age
    zhang      16
    li        17
    meng      19
    zhu        20
要求查询结果是
xh  name      age
1    zhang      16
2    li        17
3    meng      19
4    zhu        20
[/Quote]


你自己没有ID字段?
如果没有ID字段,则可以增加一个自增字段.
bitshengwuyixue 2009-07-28
  • 打赏
  • 举报
回复
假设表a 中有两列数据 name 和 age
其中 name age
zhang 16
li 17
meng 19
zhu 20
要求查询结果是
xh name age
1 zhang 16
2 li 17
3 meng 19
4 zhu 20
bitshengwuyixue 2009-07-28
  • 打赏
  • 举报
回复
还是不懂,用row_number() over (order by a) 和三楼的select count(1) 均有错,怎么回事???
izbox 2009-07-28
  • 打赏
  • 举报
回复

select * from (select *,row_number() over (order by a) '行数' from #a) as b
where b='2009-07-01'

-------------结果---------------
a b c 行数
2 2009-07-01 00:00:00.000 3 4
  • 打赏
  • 举报
回复
row_number()

需要2005及以上版本支持。
  • 打赏
  • 举报
回复
sql2000测试通过。
izbox 2009-07-28
  • 打赏
  • 举报
回复

Create table #a ( a int,b datetime,c int)
insert into #a
select 1 , '2009-06-01' , 1 union all
select 2 , '2009-06-01' , 2 union all---------------------要过滤掉的数据
select 2 , '2009-06-02' , 3 union all
select 2 , '2009-07-01' , 3 union all
select 3 , '2009-06-01' , 2

select *,row_number() over (order by a) '行数' from #a
  • 打赏
  • 举报
回复

if object_id('test') is not null
drop table test
create table test
(
id int primary key
)

insert into test
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11

select *,(select count(1) from test where id <= t.id) from test t

/**
id
----------- -----------
1 1
3 2
5 3
8 4
10 5
11 6

(所影响的行数为 6 行)
**/

yygyogfny 2009-07-28
  • 打赏
  • 举报
回复
select c1,c2,row_number() over (order by ids) as ids from tb
  • 打赏
  • 举报
回复
沙发。
bitshengwuyixue 2009-07-28
  • 打赏
  • 举报
回复
谢谢楼上的,但是没有分给你了。
加载更多回复(12)

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧