Swapping column values in Oracle

冷暖自知 提交于 2019-12-04 00:55:36
René Nyffenegger

There's no need to have three update statements, one is sufficient:

UPDATE temp_table_new 
SET    id1 = id2, 
       id2 = id1; 
Ashutosh SIngh
CREATE TABLE Names
(
F_NAME VARCHAR(22),
L_NAME VARCHAR(22)
);

INSERT INTO Names VALUES('Ashutosh', 'Singh'),('Anshuman','Singh'),('Manu', 'Singh');

UPDATE Names N1 , Names N2 SET N1.F_NAME = N2.L_NAME , N1.L_NAME = N2.F_NAME 
WHERE N1.F_NAME = N2.F_NAME;

SELECT * FROM Names;
sharma

Before:

select * from employ;

EMPNO FNAME      LNAME
----- ---------- ----------
 1001 kiran      kumar

 1002 santosh    reddy


update employ e set fname=(select lname from employ where empno=e.empno),
                    lname=(select fname from employ where empno=e.empno);

After:

 select * from employ;

 EMPNO FNAME      LNAME
------ ---------- ----------
  1001 kumar      kiran

  1002 reddy      santosh
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!