Anda dapat melakukannya dengan membuat pemicu untuk menyisipkan pada tabel1. Kemudian Anda memilih semua AddressId dari tabel2 yang belum disetel di tabel1, urutkan secara acak dan pilih yang pertama. Contoh kerja pemicu seperti itu:
CREATE TRIGGER TRIGGER1
BEFORE INSERT ON TABLE1
FOR EACH ROW /* Trigger for each new row inserted into table1 */
BEGIN
SELECT addressId INTO :new.ADDRESSID /* Set AddressId for new row in table1 */ FROM
(
SELECT table2.addressId FROM table2
LEFT JOIN table1 ON table1.CompanyNumber = table2.company AND table1.AddressID = table2.addressId
WHERE table1.AddressID IS NULL /* Not already in table1 */ AND table2.Company = :new.COMPANYNUMBER /* Select all addressIds matching the company number */
ORDER BY dbms_random.value /* order them randomly */
) hits
WHERE ROWNUM = 1; /*Only pick the first randomly ordered one*/
EXCEPTION
WHEN no_data_found THEN /* CompanyNumber not in table2 or no unique AddressId left */
:new.ADDRESSID := NULL;
END;