自增列的问题?
我想对一张表A查询之后结果自增一列。不能用临时表的方式。 问题点数:0、回复次数:10Top
1 楼clx333()回复于 2004-09-02 21:07:51 得分 0
select id=identity(int,1,1),* into #tmp from a
select * from #tmp
这种方法不行.
最好一条SQL搞定Top
2 楼pbsql(风云)回复于 2004-09-02 21:10:21 得分 0
select *,(select count(*) from t where 主键<=a.主键) id
from t aTop
3 楼hdhai9451(☆新人类☆)回复于 2004-09-02 21:16:57 得分 0
如果你的表有主健,那就可以了
select ID=(select sum(1) from tb b where b.主健<=a.主健 order by 主健),* from tb a order by 主健Top
4 楼clx333()回复于 2004-09-02 21:35:35 得分 0
楼上的好像不对;如:
id gh
1 0001
2 0002
3 0002
4 0001
5 0002
求:
1 0001
2 0001
Top
5 楼clx333()回复于 2004-09-02 21:35:59 得分 0
如果没主键怎么做?Top
6 楼hdhai9451(☆新人类☆)回复于 2004-09-02 22:07:34 得分 0
id gh
1 0001
2 0002
3 0002
4 0001
5 0002
求:
1 0001
2 0001
你在查詢的where後面沒有加條件限制
select ID=(select sum(1) from tb b where b.id<=a.id and gh='0001'order by id),gh
from tb a where gh='0001' order by id
Top
7 楼hdhai9451(☆新人类☆)回复于 2004-09-02 22:09:46 得分 0
少個表名了
select ID=(select sum(1) from tb b where b.id<=a.id and gh='0001'order by id),a.gh from tb a where gh='0001' order by id
如果沒有主鍵,那就考慮用臨時表Top
8 楼clx333()回复于 2004-09-02 22:26:44 得分 0
好像子查询中不能用order by 语句Top
9 楼panjinfu80(天蓝水晶)回复于 2004-09-03 08:30:46 得分 0
upTop
10 楼Asplin(===爱希久久&&面包会有===)回复于 2004-09-03 08:44:59 得分 0
gzTop




