2

数据库版本:oracle 11gr2,我的表是这样的:

create table t_test(t_id    number,t_value number)
insert into t_test (t_id) values (1);
insert into t_test (t_id) values (2);
insert into t_test (t_id) values (6);
insert into t_test (t_id) values (10);
insert into t_test (t_id) values (3);

t_id  t_value
 1      null
 2      null
 6      null
 10     null
 3      null

我期待的更新是这样的:

select t.t_id,row_number() over (order by t.t_id) 
from t_test t

t_id  row_number() over (order by t.t_id) 
 1      1
 2      2
 6      4
 10     5
 3      3

但是更新视图中不允许使用分析功能:

update
(select t.value,row_number() over (order by t.t_id) rn
from t_test t)
set value=rn

毫无疑问,它会导致 ORA-01732。如何放置更新查询?

4

2 回答 2

1

试试这个

update  t_test 
set t_value = ( select rn  
                   from ( select i.t_id, row_number()  over (order by i.t_id) rn  
                          from t_test i) tab 
                    where tab.t_id= t_test.t_id  )
于 2012-08-13T08:19:03.607 回答
0

此解决方案未经测试可能需要一些小改动!

创建或替换过程 SOME_PROC

BEGIN
 FOR C IN ( select t.value,row_number() over (order by t.t_id) rn
            from t_test t)

 LOOP
     BEGIN 
      UPDATE T_TEST
           SET
           T_VALUE=C.RN;
     END;

END;
于 2012-08-13T08:18:51.083 回答