首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么用SELECT 来自动生成一列序号

2012-02-17 
如何用SELECT 来自动生成一列序号如,有一表selectcol1,col2fromtb1,------------------col1col2aadfbasdfa

如何用SELECT 来自动生成一列序号
如,有一表  
select   col1,col2   from   tb1,
------------------
col1   col2
a             adf
b             asdfasa
c             adsdf
d             asd112
-----------------
想得到如下的结果,SQL   指令应如何写
t1   col1   col2
001       a             adf
002       b             asdfasa
003       c             adsdf
004       d             asd112


[解决办法]
select id=identity(int,1,1),col1,col2 into # from tb1
select * from #
[解决办法]
select id=right(identity(int,1,1)+10000,4),col1,col2 into # from tb1
select * from #
[解决办法]
2005用
SELECT ROW_NUMBER() OVER (order by col2 ) AS COL,col1,col2 from tb1
2000用
select col= identity(int,1,1),col1,col2 into # from tbl
select * from #
drop table #
[解决办法]
select id=identity(int,1,1),col1,col2 into # from tb1
select id=right(id+10000,4),col1,col2 from #

热点排行
Bad Request.