SQL自动编号 通过select生成自增长列的办法 应用在各种SQL排名中

来源:互联网 发布:域名与二级域名 编辑:程序博客网 时间:2024/04/29 19:46

內容轉自:http://db001.com/webc/mssqld/20100730197.html

SQL自动编号 创建测试环境

create table test(id int,sort varchar(10))

SQL自动编号 插入测试数据

insert into test select 1,'a' union all select 2,'b' union all select 3,'c'union all select 4,'d'union all select 5,'e'

SQL自动编号 方法1 不借助临时表

select autoid =(select count(*) from test where id <= a.id),* from test a

SQL自动编号 查询结果

autoid id  sort-----------------11a22b33c44d55e

SQL自动编号 方法2 借助临时表

select autoid=identity(int,1,1),* into # from test

SQL自动编号 方法2查询结果

select * from #----------------------autoid id  sort-----------------11a22b33c44d55e

SQL自动编号 建议

实际应用中,建议使用方法1,这种一般应用在为各类排名中使用。
原创粉丝点击