查询语句中添加自动编号

来源:互联网 发布:python时间序列分析包 编辑:程序博客网 时间:2024/04/30 00:35

1、使用一条sql语句,原理是在结果中查询大于等于该记录的记录跳数。

   如:select count(*) as Rank,a1.urlname from urlconfig a1,urlconfig a2 where a1.urlname>a2.urlname
      group by a1.urlname order by Rank 。

  缺点:性能不好;如果存在相同记录,就会出现并列情况。
  2、sql提供了identity function,可以获得标识列的值,不过这个函数只能用于select into语句,所以需要引入临时表
     select identity(int,100,1)as rank,urlname into #tmp from urlconfig select * from #tmp drop table #tmp
     drop table #tmp

   这种方法性能和适用性都比第一种方法号,缺点是必须通过几条sql语句才能完成。一般还是建议在客户端完成。

原创粉丝点击