tableA
id     name
10     天在
11     在有
12     中有
....
查询它,如 select * from tableA希望出现
xxx        id        name
1          10        天在
2          11        在有
3          12        中有
....
xxx 字段自动以结果的行数为值不希望在程序中实现,而直接使用sql语句

解决方案 »

  1.   

    select row_number() over(order by id),* from tableA
    要求是SQL2005的版本以上才行,否则不可以。
    只能采用插入临时表或表变量的方式,然后再进行查询才可以。
      

  2.   

    --测试数据
    declare @tableA table(id int, name char(10))
    insert @tablea
    select 11,    '在有' union all
    select 10,    '天在' union all
    select 12,    '中有' --查询SQL
    select (select count(1) from @tablea t where t.id <= a.id) as xxx, id, name
      from @tablea a order by xxx
    /*
    xxx         id          name       
    ----------- ----------- ---------- 
    1           10          天在      
    2           11          在有      
    3           12          中有      
    */
      

  3.   

    能不能不固定排序方式,因为排序是不固定的,而且我这个要求,就是因为我的排序条件太复杂,所以在分页的时候能简化才想要这个功能的SELECT     shopList.shopid, shopList.shopName, userList.tel, userList.address,qz
    FROM         userList INNER JOIN
                          shopList ON userList.userid = shopList.shopid
    WHERE     (shopList.shopName LIKE '%公司%') OR
                          EXISTS
                              (SELECT     1 AS Expr1
                                FROM          keyAdList
                                WHERE      (adKey = '家具') AND (shopid = userList.userid))
    ORDER BY 
                              (SELECT     TOP (1) money
                                FROM          keyAdList
                                WHERE      (adKey = '家具') AND (shopid = userList.userid)
                                ORDER BY money DESC) DESC, userList.userid DESC像这个语句的分页操作
      

  4.   

    select identity(int,1,1),id,name into #s from tableA order by id
    select * from #s这样应该可以