--Order 20015506.70

ALTER TABLE UNIQUEITEM ADD (REPLACEMENTWORKORDER_ID NUMBER NULL);


update uniqueitem ui
set    ui.replacementworkorder_id =
        (select wol.workorder_id
         from   uniqueitemnonpool uip, workorderline wol
         where  uip.replacementworkorderline_id = wol.workorderline_id
         and    uip.uniqueitem_id = ui.uniqueitem_id)
where  ui.poolitem = 2
and    exists (select wol.workorder_id
        from   uniqueitemnonpool uip, workorderline wol
        where  uip.replacementworkorderline_id = wol.workorderline_id
        and    uip.uniqueitem_id = ui.uniqueitem_id);


CREATE INDEX XIF10UNIQUEITEM ON UNIQUEITEM (REPLACEMENTWORKORDER_ID);

ALTER TABLE UNIQUEITEM
  ADD CONSTRAINT UNIQUEITEM_REPLACEWO FOREIGN KEY (REPLACEMENTWORKORDER_ID)
  REFERENCES WORKORDER (WORKORDER_ID) ON DELETE SET NULL;

commit;