I am exporting client data from a database into Salesforce. I have two tables (Fiddle shown below)
Table: SALESFORCE_CONTACTS
user_id | start_at | end_at | company_id |
---|---|---|---|
37483 | 2021-12-12 | NULL | 88 |
23982 | 2020-03-02 | 2021-04-02 | 169 |
Table: DB_CONCATS
user_id | start_at | end_at | company_id |
---|---|---|---|
37483 | 2021-12-12 | NULL | 88 |
23982 | 2020-03-02 | 2021-04-02 | 169 |
Every so often a column in DB_CONCATS
will change and on the export (every 2 hours) the Salesforce data will change. Right now I am exporting every single contact every 2 hours.
But I only want to export the records that do not match the Salesforce table. The only way I can think of is to write something like this:
SELECT
DB_CONTACTS.*
FROM
DB_CONTACTS
LEFT JOIN
SALESFORCE_CONTACTS ON DB_CONTACTS.user_id = SALESFORCE_CONTACTS.DB_USER_ID
WHERE
DB_CONTACTS.user_id <> SALESFORCE_CONTACTS.db_user_id
OR DB_CONTACTS.created_at <> SALESFORCE_CONTACTS.db_created_at
OR DB_CONTACTS.disabled_at <> SALESFORCE_CONTACTS.db_disabled_at
OR DB_CONTACTS.company_id <> SALESFORCE_CONTACTS.db_company_id
Great! That produces no records because the two tables are exactly alike at the moment. Once the end_at
in DB_CONCATS
is filled out a record will appear in the query and then, on the next extract, it will transfer it over to SALESFORCE_CONTACTS
The trouble is that the sales team is constantly adding new fields to transfer data to. So every time that is done I would have to add a line to the query above and it would be trouble to maintain. Is there an easier way to compare the two tables without so many lines of code?
http://sqlfiddle.com/#!9/3ca536/9
Thank you