日期:2014-05-19  浏览次数:20601 次

求一简单的问题!!
比如表A  
col   col2
54   a  
57   b
59   c
60   d
其中   col   为主键   不重复
我要想select   这样结果   ,不要该变表的结构
newcol     col   col2
      0         54   a  
      1         57   b
      2         59   c
      3         60   d
请问该怎么做了?

  0


------解决方案--------------------
drop table A
go
create table A(col int,col2 varchar(10))
insert into A
select 54, 'a '
union all select 57, 'b '
union all select 59, 'c '
union all select 60, 'd '

select newcol=(select count(*) from A aa where aa.col <A.col),* from A order by col
/*
newcol col col2
----------- ----------- ----------
0 54 a
1 57 b
2 59 c
3 60 d

(所影响的行数为 4 行)
*/
------解决方案--------------------
select identity(int,1,1) as id, [user_name] into #tt from [User]
select id - 1 as id, [user_name] from #tt
drop table #tt