2011-07-15 38 views
1

我有一个Oracle DB,其表名为myC。在这张表中我有几行,其中两个叫做myCheight, myCwidthOracle SQL:比较2列中的所有值并交换它们

我需要阅读这些值,并将它们像IF myCheight > myCwidth DO switch the values.

我试图从一个行读取值进行比较,但没有得到它的工作。我使用Oracle Oracles Oracle SQL Developer。

这就是我想出迄今:

set serveroutput on; 
DECLARE 
    cursor h is select * from MyC; 
    type htype is table of h%rowtype index by number; 

    stage_tab htype; 
    master_tab htype; 
BEGIN 
    open h; 
    loop 
     fetch h bulk collect into stage_tab limit 500; 

     for i in 1 .. stage_tab.count loop 
      master_tab(stage_tab(i).id) := stage_tabe(i); 
     end loop; 

     exit when h%notfound; 
    end loop; 
    close h; 

end; 

回答

10

你就不能这样做呢?

UPDATE myC 
    SET myCheight = myCwidth, 
     myCwidth = myCheight 
    WHERE myCheight > myCwidth 
+0

+1:同意。 'my myCheight = myCwidth'在'myCwidth = myCheight'之前没有完成,它们'出现'同时发生。 – MatBailie

+0

+1。你也可以添加一个标志:*“不要在家里试用(或MySQL)”* –

+0

不错,它工作。 – nottinhill