select
1+isnull((select count(*) from tablename
where col1<a.col1
or (col1=a.col1 and col2<a.col2)
or (col1=a.col1 and col2=a.col2 and col3<a.col3)
...
),0) as 序号
,*
from tablename a
order by col1,col2,...
select
1+isnull((select count(*) from tablename
where col1<a.col1
or (col1=a.col1 and col2<a.col2)
or (col1=a.col1 and col2=a.col2 and col3<a.col3)
...
),0) as 序号
,*
from tablename
order by col1,col2,...
1:
Select identity(int,1,1) as iid,* into #tmp from 表
select * from #tmp
2: 如果有关键字段
Select (select sum(1) where keyField <= a.keyField) as iid,* from 表 a