如何在查询到的结果集前面加入序号?
下面是查询结果
sbbh dm
B-002 0100
B-003 0901
B-005 0500
B-009 0500
B-010 0500
B-011 0500
我想变成以下的
id sbbh dm
1 B-002 0100
2 B-003 0901
3 B-005 0500
4 B-009 0500
5 B-010 0500
6 B-011 0500
这样的sql怎么写?
问题点数:100、回复次数:4Top
1 楼klan(因帅被判7年)回复于 2005-05-24 10:48:53 得分 20
try:
CREATE TABLE #t(id int identity(1,1),sbbh varchar(5),dm varchar(4))
go
INSERT INTO #t select sbbh, dm from table1
go
SELECT * FROM #t
go
drop table #t
Top
2 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-05-24 10:52:29 得分 40
SQL SERVER 2000不支持这个功能,只能在外部程序编号或者借助临时表;
借助临时表:
select identity(int,1,1) rownum,* into #t from tabname where ...
select * from #t
SQL Server 2005提供了ROWNUM函数。
Top
3 楼ziping(子平)回复于 2005-05-24 10:52:51 得分 20
select *,(select count(*) from 表 where sbbh<=b.sbbh ) as id from 表 bTop
4 楼hdhai9451(☆新人类☆)回复于 2005-05-24 10:56:06 得分 20
create table tb(sbbh varchar(10),dm varchar(10))
Insert into tb
select 'B-002','0100'
union all select 'B-003','0901'
union all select 'B-005','0500'
union all select 'B-009','0500'
union all select 'B-010','0500'
union all select 'B-011','0500'
select id=(select count(*) from tb where sbbh<=a.sbbh),* from tb a
1 B-002 0100
2 B-003 0901
3 B-005 0500
4 B-009 0500
5 B-010 0500
6 B-011 0500
Top




