简体   繁体   中英

Oracle 11G create loop and trigger though table bulk insert/update

Oracle 11G

Apex 4.2.6

below creates a 2 year work log for each engineer (:ENG_ID) APEX(form field). But now require to add a work log for a further 250-500 engineers. How can I loop though each :ENG_ID for existing staff and a trigger for new

merge INTO MD_TS_MAST D 
USING (SELECT :ENG_ID AS eng_id, 
          dt 
   FROM   ALL_DATES) s 
ON (D.eng_id= s.eng_id AND d.ms_date = s.dt) 
WHEN NOT matched THEN 
INSERT (D.eng_id, 
      D.ms_date) 
VALUES (s.eng_id, 
      s.dt); 

Many thanks

Given that the ENG table has all the ENG_ID values and ALL_DATES has all the date values you could do something like:

MERGE INTO MD_TS_MAST d
  USING (SELECT e.ENG_ID,
                a.DT 
           FROM ENG e
           CROSS JOIN ALL_DATES a) s 
    ON (d.ENG_ID = s.ENG_ID AND
        d.MS_DATE = s.DT) 
  WHEN NOT MATCHED THEN 
    INSERT (ENG_ID, 
            MS_DATE) 
    VALUES (s.ENG_ID, 
            s.DT); 

Share and enjoy.


EDIT

It sounds like what you're trying to do is add new rows to MD_TS_MAST when a new engineer is added to ENG. In that case an ON INSERT trigger on MD_TS_MAST would seem to be the best solution:

CREATE TRIGGER MD_TS_MAST_AI
  AFTER INSERT ON MD_TS_MAST
  FOR EACH ROW
BEGIN
  MERGE INTO MD_TS_MAST d
    USING (SELECT :NEW.ENG_ID,
                  a.DT 
             FROM ALL_DATES a) s 
      ON (d.ENG_ID = s.ENG_ID AND
          d.MS_DATE = s.DT) 
    WHEN NOT MATCHED THEN 
      INSERT (ENG_ID, 
              MS_DATE) 
      VALUES (s.ENG_ID, 
              s.DT); 
END MD_TS_MAST_AI;

Best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM