2

我想去掉列_中的所有前缀name,但结果可能会导致冲突。因此,如果结果与现有结果重复,我想给_它加上后缀,直到没有重复。

在下面的示例中,_test应重命名为test___.

create table A  
(  
  name  VARCHAR2(20) unique,  
  id int  
);  
insert into a (name, id) values ('_test', 1);  
insert into a (name, id) values ('test', 2);  
insert into a (name, id) values ('test_', 3);  
insert into a (name, id) values ('test__', 4);  
4

1 回答 1

5

试试这个:

merge into A
using (with aa as (select id, trim('_' from name) name from A)
     select rpad(name,
               length(name) - 1 + row_number()
               over(partition by name order by id),
               '_') name2,
          id
     from AA) s
on (s.id = a.id)
when matched then
  update set a.name = s.name2
于 2012-06-11T06:24:00.533 回答