简体   繁体   中英

how can I take two tables and merge them into a third without a “temp” table?

I have a few oracle strings that is doing a merge and an inset and then merge. I got it to work this way but due to company restrictions, adding tables takes at least two weeks. (I made the site_item_temp table just to get this command set to work.) The oracle Server does not have this table and we cannot sit and wait while the DBAs take their time adding the table, or any table that is with the proper privileges and such.

I need this to run without hitting the site_item_temp table, or any other table except those listed. due to DBA's restrictions/time limits. I'm scratching my head with this. it is probably very simple but I'm just drawing a blank.

Here are the scripts I run.

MERGE INTO SITE_ITEM_MASTER D 
 USING ITEM_MST S 
    ON (D.SKU = S.INVEN_ID 
    and d.loc_id = s.loc_id)

WHEN NOT MATCHED THEN 
INSERT (D.SKU, D.CASES_PER_PALLET, D.LOC_ID) 
VALUES (S.INVEN_ID, S.CASE_PER_PAL_QTY, S.LOC_ID);



  DELETE FROM SITE_ITEM_TEMP;



  INSERT INTO SITE_ITEM_TEMP ( SKU, CASES_PER_PALLET, LOC_ID )
 SELECT ITEM_MST.INVEN_ID, AVG(ITEM_MST.CASE_PER_PAL_QTY) AS AVGOFCASE_PER_PAL_QTY, 
   ICAM_LOCATIONS.LOCATION_ID
   FROM ITEM_MST, ICAM_LOCATIONS
  GROUP BY ITEM_MST.INVEN_ID, ICAM_LOCATIONS.Location_ID;



  MERGE INTO SITE_ITEM_MASTER D 
 USING SITE_ITEM_TEMP S
    ON (D.SKU = S.SKU 
    AND D.LOC_ID = S.LOC_ID)

  WHEN NOT MATCHED THEN 
INSERT (D.SKU, D.CASES_PER_PALLET, D.LOC_ID) 
VALUES (S.SKU, S.CASES_PER_PALLET, S.LOC_ID);



  DELETE FROM SITE_ITEM_TEMP;

thank you.

merge into site_item_master d 
 using item_mst s 
    on (d.sku = s.inven_id     
    and d.loc_id = s.loc_id)
when not matched then 
    insert (d.sku, d.cases_per_pallet, d.loc_id) 
    values (s.inven_id, s.case_per_pal_qty, s.loc_id);    

merge into site_item_master d 
 using (
       select item_mst.inven_id sku, avg(item_mst.case_per_pal_qty) as cases_per_pallet, icam_locations.location_id loc_id                
         FROM ITEM_MST, ICAM_LOCATIONS
        group by item_mst.inven_id, icam_locations.location_id
       ) s 
    on (d.sku = s.sku and d.loc_id = s.loc_id)
  when not matched then   
    insert (d.sku, d.cases_per_pallet, d.loc_id) 
    values (s.sku, s.cases_per_pallet, s.loc_id);

You can use subqueries for USING. I just replaced tmp table with you select (used for insert into tmp)

Actually, you don't need a MERGE

insert into site_item_master (sku, cases_per_pallet, loc_id) 
select inven_id, case_per_pal_qty, loc_id from(
  select inven_id, case_per_pal_qty, loc_id 
    from item_mst
  union all
  select item_mst.inven_id sku, avg(item_mst.case_per_pal_qty), icam_locations.location_id
   from item_mst, icam_locations 
  group by item_mst.inven_id, icam_locations.location_id
) s
where not exists (select 1 from site_item_master d where d.sku = s.inven_id and d.loc_id = s.loc_id);

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