怎么样实现动态的在结果集里增加一个序号的列,序号为加1递增的?求助

alex5006 2005-08-02 10:04:05
怎么样实现动态的在结果集里增加一个序号的列,序号为加1递增的?
比如表t1里有两个字段f1和f2
结果是

1 a1 b1
2 a2 b2
3 a3 b3
4 a4 b4
...全文
278 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2005-08-02
  • 打赏
  • 举报
回复
declare @tb table(f1 varchar(10),f2 varchar(10))
insert @tb
select 'a1','b1' union
select 'a2','b2' union
select 'a3','b3' union
select 'a4','b4' union
select 'a5','b5'

select [ID]=(select count(1) from @tb where f1<=t.f1),
*
from @tb t
order by f1

--结果
/*
ID f1 f2
----------- ---------- ----------
1 a1 b1
2 a2 b2
3 a3 b3
4 a4 b4
5 a5 b5

(所影响的行数为 5 行)
*/
alex5006 2005-08-02
  • 打赏
  • 举报
回复
是在结果集里动态地增加列,不是在数据库里添加一个字段,如果能加字段那太简单了吧
GRLD8888 2005-08-02
  • 打赏
  • 举报
回复
----你可以借用临时表
---具体可以这样做:

select identity(int,1,1) id,* into #a from t1
drop table t1
select * into t1 from #a
drop table #a
select * from t1

--或者你也可以这样做:

select (select count(*) from t1 where a.f1>=f1 ) id,* from t1 a
lcooc 2005-08-02
  • 打赏
  • 举报
回复
alter table tablename add col int (identity 1,1)
masse 2005-08-02
  • 打赏
  • 举报
回复
CREATE TABLE t_test(
id INTEGER IDENTITY
PRIMARY KEY,
name VARCHAR(10)
);
INSERT INTO t_test(name) VALUES ('Andrew');
INSERT INTO t_test(name) VALUES ('Gordon');
SELECT * FROM t_test;

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧