-- TD 21383

update customer c
   set c.serviceteam_id =
       (select st.serviceteam_id
          from serviceteam st, businessunit bu, plant p
         where st.plant_id = p.plant_id
           and p.plant_id = bu.plant_id
           and bu.businessunit_id = c.defaultbusinessunit_id
           and st.code =
               (select st2.code
                  from serviceteam st2
                 where st2.serviceteam_id = c.serviceteam_id))
 where c.serviceteam_id not in
       (select st.serviceteam_id
          from serviceteam st, businessunit bu, plant p
         where st.plant_id = p.plant_id
           and p.plant_id = bu.plant_id
           and bu.businessunit_id = c.defaultbusinessunit_id)
   and c.serviceteam_id not in
       (select st.serviceteam_id
          from serviceteam        st,
               businessunit       bu,
               plant              p,
               customeractivities ca
         where st.plant_id = p.plant_id
           and p.plant_id = bu.plant_id
           and bu.businessunit_id = ca.processbusinessunit_id
           and ca.customer_id = c.customer_id);
commit;
