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

求一简单的有关问题!

2012-01-28 
求一简单的问题!!比如表Acolcol254a57b59c60d其中col为主键不重复我要想select这样结果,不要该变表的结构n

求一简单的问题!!
比如表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

热点排行