2016-05-11 21 views
-1

問題があります。oracle sqlの更新が機能していない100%

ここに私のテーブル。

--tableA-- 

id | number | step | limit | last_update 
------------------------------------------------------- 
1 | 0  | 0  | 5  | 10-05-2016 08:00:00 


--tableB-- 

id | number | step | last_update 
-------------------------------------------- 
1 | 1  | 1  | 10-05-2016 08:00:00 

ここに私のコード。

for cur in 
(
     select id,number,step,limit,last_update 
     from 
      tableA 
) 
loop 
     if cur.number < cur.limit then 

      cur.number := mod(cur.number,3) + 1; --calculator cur.number increase from 1 -> 3 

      insert into tableB(id,number,step,last_update) -- INSERT OK 
      values (cur.id,cur.number,cur.step + 1,cur.last_update); 
      commit; 

      cur.step := cur.step + trunc(cur.number/3); --calculator cur.step increase by cur.number 
      -- here : cur.number = 1 and cur.step = 1 

      update tableA 
      set 
        number = cur.number  -- not update 
        ,step = cur.step  -- not update 
        ,last_update = sysdate -- update ok 
      where id = cur.id 
      commit; 

     end if; 
end loop; 

cur.number = 1とcur.step = 1ラン更新コマンドの前

理由IDとにtableA = 1のみLAST_UPDATE、番号を更新し、更新しないステップ。更新にtableA後

:番号= 0、ステップ= 0

更新コード

for cur in 
(
     select id,number,step,limit,last_update 
     from tableA 
) 
loop 
     if cur.number < cur.limit then 

      cur.number := mod(cur.number,3) + 1; --calculator cur.number increase from 1 -> 3 

      insert into tableB(number,step,last_update) -- INSERT OK 
      values (cur.number,cur.step + 1,cur.last_update); 
      commit; 

      -- cur.step := cur.step + trunc(cur.number/3); --calculator cur.step increase by cur.number 
      -- here : cur.number = 1 and cur.step = 1 

      select number,step into temp_number,temp_step 
      from tableB where id = cur.id; 

      update tableA 
      set 
        number = temp_number -- not update 
        ,step = (temp_step - 1) + trunc(temp_number/3) -- not update 
        ,last_update = sysdate -- update ok 
      where id = cur.id 
      commit; 

     end if; 
end loop; 

私を助けてください。

ありがとうございます。

+0

カーソルの値は更新できません。新しい値を変数に入れる必要があります。 :) –

+0

cur.number = 1かつcur.step = 1?で成功tableAを挿入するのはなぜですか? – user2964569

+0

問題は正しく更新されていますか? –

答えて

0

は、手順を見てみましょう:

私、あなたが行を挿入し、その後、あなたが、最初

IIを開きます。何、CURSORを持っている:

 insert into tableB(number,step,last_update) -- INSERT OK 
     values (cur.number,cur.step + 1,cur.last_update); 
     commit; 

IIIがあり、不要なSELECT INTO:

 select number,step into temp_number,temp_step 
     from tableB where id = cur.id; 

IV、最終的にはUPDATE:

ステップiii、およびステップivで値が同じであるため、現在のIDと同じ値をUPDATEしているため、SELECT INTOは不要です。

カーソルは処理中に一定の結果セットです。挿入後に変更されることはありません。

+0

カーソルの値を格納してexcute updateコマンドに入れると。テーブルAは更新されますか? – user2964569

+0

はい、テーブルは更新されますが、カーソルは更新前にすでに結果セットを読み込んでいるため、更新された値は含まれません – Thomas

関連する問題