Question

Pardon the title as I didn't know how to put it up in title.

I have a table staging_cust_acct (naics_cd, naics_desc varchar, is_pep, phone_num, is_updated, update_date)

I wrote below statements to update this table. I need to use the code to update only those columns which have NOT been previously updated (for which I have the is_updated (0 or 1) and update_date columns)

In case you are wondering why I made separate update statements, it is because there are many more update statements which have dependencies (like UPDATE 2 statement needs to be run after UPDATE 1 only)

What can I add in below SQL statements to apply the above mentioned logic?

-- Update NAIC code and description
UPDATE S 
SET    s.naics_cd = left(n.naic, 6), 
       s.naics_desc = substring(n.naic, 9, 255) 
from   mtb..staging_cust_acct s 
       right outer join mtb_aml..tb_party_kyc e 
                     on e.customerinternalid = s.party_key 
       right outer join mtb_aml..tb_ref_naic n 
                     on e.d_case_id = n.d_case_id   

 -- Update is_PEP Flag
 UPDATE S 
 SET    s.is_pep = '1' 
 from   mtb..staging_cust_acct s 
        right outer join mtb_aml..tb_party_kyc e 
                      on e.CustomerInternalID = s.party_key 
        right outer join mtb_aml..tb_ref_pep_details p 
                     on e.d_case_id = p.d_case_id 

 -- Update party phone number
 UPDATE S 
 SET    s.phone_num = pc.phone 
 from   mtb..staging_cust_acct s 
        right outer join mtb_aml..tb_party_kyc e 
                      on s.party_key = e.customerinternalid 
        right outer join mtb_aml..tb_ref_primary_contact pc 
                     on pc.d_case_id = e.d_case_id  
Was it helpful?

Solution

Add where condition in sql to check is_updated (0 or 1) and update_date like:

UPDATE S 
SET    s.naics_cd = left(n.naic, 6), 
       s.naics_desc = substring(n.naic, 9, 255) 
from   mtb..staging_cust_acct s 
       right outer join mtb_aml..tb_party_kyc e 
                     on e.customerinternalid = s.party_key 
       right outer join mtb_aml..tb_ref_naic n 
                     on e.d_case_id = n.d_case_id
       WHERE s.is_updated=0               --Added Condition
              AND s.update_date IS NULL     --Added Condition

Here, I am assuming is_updated=0 means not updated and update_date is nullable & set only when value is edited.

If above assumption is not good for you, you can change where condition accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top