I have a Maximo database that has a table structure that I cannot change. I am looking to copy the primary email address to the PERSON table anytime it is created or updated. The following structure describes the PERSON table and the EMAIL table
PERSON table:
PERSONID | EMAIL | ...(other irrelevant columns)...
EMAIL table:
PERSONID | EMAILADDRESS | ISPRIMARY | ...(other irrelevant columns)...
As you can see, the two tables are linked in the PERSONID column. Here is what I would like to do with the trigger:
If the EMAIL table is updated or a new row is inserted, I would like to copy the EMAILADDRESS field to the corresponding record (as indicated by PERSONID) in the PERSON table, if the ISPRIMARY field is 1 (1 means primary , 0 means secondary).
I have not written many triggers, so I want to make sure that I only look at the rows that are updated or inserted into the EMAIL table, and only update the PERSON table if there is a new / updated primary email address. Thank you in advance for your help!
UPDATE 1:
After looking at Cade's answer, here is the trigger that I am starting to shape:
CREATE TRIGGER EMAIL_update ON UPDATE,INSERT AS BEGIN
UPDATE p
SET p.email = i.emailaddress
FROM dbo.PERSON as p
INNER JOIN inserted AS i ON i.PERSONID = p.PERSONID AND i.isprimary=1
END
, , - .