预言机序列问题

发布于 2024-09-15 18:26:10 字数 1185 浏览 1 评论 0原文

我的触发器中有两个插入是由更新触发的。我的 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,我会向vendor_hist 表中插入三个 3,3,3 的数据。必须是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 与第二次插入中的 thID 相同?

希望这是有道理的。

There are two inserts in my trigger which is fired by an update. My Vendor_Hist table has a field called thID which is the primary key in Task_History table. thID gets its' value from 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 );      

Now, my problem is the tmpTHID always the latest value of mySeq.nextVal. So, if thID in task_history is 1,2,3, I get three inserts into vendor_hist table with 3,3,3. It has to be 1,2,3. I also tried

  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;

but then I get a "warning compiled with errors" message when I execute the trigger. How do I make sure that the thID in first insert is also the same in my second insert?

Hope it makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-09-22 18:26:10
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)。

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;

I'm assuming the columns inserted in the second insert are from the VENDORS table; if not, the referencing cursor (i or j) should be used as appropriate.

我们的影子 2024-09-22 18:26:10

它与以下子选择一起使用,而不是 currVal。

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

Instead of the currVal, it works with the following subselect.

   (  select min(thID) from task_history t3
       where t3.project_id = t2.project_id
       and t3.myID = t2.myID 
       and t3.version = tmpVersion ), 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文