有一个表已经建立了一个标识列,不过现在在查询中存在条件select * from table a where ...,当然标识列也就不连续.现在想获得的是一个顺序增长连续的如何处理,结果如下
a  b  1
d  d  2
4  s  3
s  4  4
使用newid()不能获得整数,使用select identity(int,1,1)结果在查询中不能使用,不知道有啥办法?

解决方案 »

  1.   

    select * ,
    (select count(*) from tablename where 条件 and 标识列<=a.标识列) as 序号
    from tablename a
    where 条件
    order by 标识列
      

  2.   


    select (select count(*) from authors where au_id <a.au_id),* from authors a order by au_id
      

  3.   

    sql 2005就可以直接用row_number()函数了.
      

  4.   

    SELECT @@IDENTITY AS 'Identity'要用identity(int,1,1) TID,一定要用虚拟表。。select identity(int,1,1) TID,* into #Temp from TableName