在DB2中,曾经这样用过一条SQL:
update set xh = (row_number() over(order by name)) where .....大意就是将XH字段根据name字段来排序生成一个流水。执行后,表中数据的XH字段应该如下:
XH
----
1
2
3
4
.
.
请问在oracle中,如何实现这个效果? 总是提示我禁止使用window函数。

解决方案 »

  1.   

    oracle中
    update table set xh=rownum where .....
    可以近似达到要求的效果,但是rownum不能按照指定的字段来生成序号。有人知道如何解决这个问题么
      

  2.   

    select row_number() over (order by 排序字段) from tab

    tab(a,b)
    aa,aa
    bb,aa
    a1,bb
    按 排序字段 排序
    select row_number() over (order by b),a from tab
    结果
    1,aa
    2,aa
    1,bb
      

  3.   

    试试:
    update 要更新的表 set xh = (select rownum from 要更新的表 where 二者对应关系 order by name)
    where .....
      

  4.   

    我在oracle8以上试了:
    create table aa (a number(4),b char(1));
     declare
       i int;
     begin
       for i in 1..4 loop
         insert into aa (b) values (chr(64+i));
       end loop;
       insert into aa(b) values ('A');
       commit;
     end;
    /
    UPDATE aa SET a=(SELECT xh FROM (SELECT min(ROWNUM) AS xh,b FROM aa GROUP BY b ORDER BY b) a WHERE aa.b=a.b);
    commit;
    select * from aa
    结果为:
             A BB
    ---------- -
             1 A
             2 B
             3 C
             4 D
             1 A
      

  5.   

    这样可以,不过select 多了一点
    update test x set x.n = (select y.num from (select rownum as num,a from (select a  from test order by a))  y where x.a=y.a )