--order 502665
UPDATE invoiceheaderextrainfo ihei
   SET ihei.paybycustomer_link_id = (SELECT cbi.paybycustomer_link_id
                                       FROM invoiceheader       ih,
                                            customerbillinginfo cbi,
                                            customer            cu
                                      WHERE ih.invoiceheader_id =
                                            ihei.invoiceheader_id
                                        AND cbi.customer_id = cu.customer_id
                                        AND cu.customer_id =
                                            ih.billtocustomer_link_id
                                        AND cbi.paybycustomer_link_id IS NOT NULL)
 WHERE ihei.paybycustomer_link_id IS NULL
AND EXISTS (select * from databasesettings d where d.code = 'AMM');

commit;
