oracle序列问题

时间:2010-08-24 14:19:20

标签: oracle10g

我的触发器中有两个插件,由更新触发。我的Vendor_Hist表有一个名为thID的字段,它是Task_History表中的主键。 thID从mySeq.nextval获取其值。

INSERT INTO TASK_HISTORY
    ( thID, phId, LABOR, VERSION )         

    ( select   mySeq.NEXTVAL, mySeq2.CurrVal,  LABOR,  tmpVersion 
      from   tasks t     
      where t.project_id = :new.project_ID ); 

  select mySeq.currval into tmpTHID from dual; -- problem here! 

   INSERT INTO VENDOR_HIST 
    ( vhID, thID, Amount, Position, version  ) 

    ( select mySeq3.NEXTVAL,   tmpTHID,  
                Amount, Position, tmpVersion
      from   vendors v2, tasks t2     
      where  v2.myID =  t2.myID 
      and      t2.project_id = :new.project_ID );      

现在,我的问题是tmpTHID始终是mySeq.nextVal的最新值。因此,如果task_history中的thID为1,2,3,我将使用3,3,3在vendor_hist表中获得三次插入。它必须是1,2,3。我也试过

  INSERT INTO TASK_HISTORY
    ( thID, phId, LABOR, VERSION )         

    ( select   mySeq.NEXTVAL, mySe2.CurrVal,  LABOR,  tmpVersion 
      from   tasks t     
      where t.project_id = :new.project_ID )  returning thID into :tmpTHID;

但是当我执行触发器时,我收到“带错误编译的警告”消息。如何确保第一次插入中的thID在我的第二次插入中也是相同的?

希望它有意义。

2 个答案:

答案 0 :(得分:0)

for i in (select * from tasks t
           where t.project_id = :new.project_id)
loop
  insert into task_history
  ( thID, phId, LABOR, VERSION )              
  values
  (mySeq.NEXTVAL, mySeq2.CurrVal,  i.LABOR,  i.tmpVersion);

  for each j in (select * from vendors v
                  where i.myId = v.myId)
  loop
    insert into vendor_history
    ( vhID, thID, Amount, Position, version  )         
    values    
    (mySeq3.NEXTVAL, mySeq.CURRVAL, j.Amount, j.Position, j.tmpVersion)
  end loop;
end loop;

我假设第二个插入中插入的列来自VENDORS表;如果不是,则应适当使用引用光标(i或j)。

答案 1 :(得分:0)

它不使用currVal,而是使用以下子选项。

   (  select min(thID) from task_history t3
       where t3.project_id = t2.project_id
       and t3.myID = t2.myID 
       and t3.version = tmpVersion ),