oracle sql更新无法正常工作100%

时间:2016-05-11 02:29:24

标签: oracle oracle11g oracle-sqldeveloper

我有一个问题。

这是我的表。

--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

为什么tableA id = 1只更新last_update,number和step not update。

更新tableA后:number = 0,step = 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;

请帮帮我。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

让我们看看步骤:

我,你有一个游标,你将首先打开什么

ii,然后你插入一行:

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

iii,有一个不必要的SELECT INTO:

        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;

iv,最后是UPDATE:

Error: java.io.IOException: wrong value class: org.apache.mahout.math.VarLongWritable is not class org.apache.mahout.math.VectorWritable
    at org.apache.hadoop.io.SequenceFile$Writer.append(SequenceFile.java:1378)
    at org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat$1.write(SequenceFileOutputFormat.java:83)
    at org.apache.hadoop.mapred.ReduceTask$NewTrackingRecordWriter.write(ReduceTask.java:558)
    at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
    at org.apache.hadoop.mapreduce.lib.reduce.WrappedReducer$Context.write(WrappedReducer.java:105)
    at org.apache.hadoop.mapreduce.Reducer.reduce(Reducer.java:150)
    at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171)
    at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627)
    at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

SELECT INTO是不必要的,因为步骤iii和步骤iv中的值相同, 您正在更新当前ID的相同值。

CURSOR在此过程中是一个常量结果集,在插入后不会改变