我想更新作业以及“ _1”,“ _ 2”,“ _ 3” ...我该怎么办?

时间:2018-08-11 13:57:00

标签: sql oracle oracle11g

这是表EMP

select * from emp

--------------------
     EMPNO  JOB      
---------- ---------
      7698 MANAGER  
      7782 MANAGER  
      7499 SALESMAN 
      7521 SALESMAN 
      7654 SALESMAN 

我想要它

---------------------
     EMPNO JOB      
---------- ---------
      7698 MANAGER_1  
      7782 MANAGER_2  
      7499 SALESMAN_3 
      7521 SALESMAN_4 
      7654 SALESMAN_5 
      .... ..........
      .... ..........

3 个答案:

答案 0 :(得分:1)

update emp a set JOB=(select JOB from (select JOB||'_'||rank() over (order by EMPNO  ) JOB,EMPNO  from emp )b where a.EMPNO =b.EMPNO )
where exists (select 1 from emp b where a.EMPNO=b.EMPNO)

将按empno顺序给出_1,_2,_3,_4

update emp a set JOB=(select JOB from (select JOB||'_'||rank() over (partition by JOB order by EMPNO  ) JOB,EMPNO  from emp )b where a.EMPNO =b.EMPNO )
where exists (select 1 from emp b where a.EMPNO=b.EMPNO)

在工作上会给出_1,_2,_3,_4

答案 1 :(得分:1)

与该更新相比,MERGE的效率更高。

MERGE INTO emp t
USING ( SELECT empno, job || '_' || dense_rank() over ( order by empno ) new_job FROM emp)  u
ON ( t.empno = u.empno )
WHEN MATCHED THEN UPDATE SET t.job = u.new_job;

答案 2 :(得分:0)

它将完成:

select empno,ename||'_'||rownum from emp;