SQL触发器更新另一个表

发布于 2024-10-10 19:15:34 字数 869 浏览 2 评论 0原文

我有一个 Maximo 数据库,其表结构无法更改。我希望在创建或更新时将主要电子邮件地址复制到 PERSON 表中。以下结构描述了 PERSON 表和 EMAIL 表


PERSON 表:

PERSONID | EMAIL | ...(other irrelevant columns)...

EMAIL 表:

PERSONID | EMAILADDRESS | ISPRIMARY | ...(other irrelevant columns)...

如您所见,这两个表在 PERSONID 列上链接。这是我希望触发器执行的操作:
如果更新 EMAIL 表或插入新行,我想将 EMAILADDRESS 字段复制到 PERSON 表上的相应条目(由 PERSONID 链接),如果 ISPRIMARY 字段等于 1(1 表示主要,0 表示中学)。
我没有编写很多触发器,因此我想确保只查看正在更新或插入到 EMAIL 表中的行,并且仅在有新的/更新的主电子邮件地址时更新 PERSON 表。预先感谢您的所有帮助!


更新1:
看完凯德的回复后,这是我开始形成的触发器:

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  

我相信只要更新或插入电子邮件表中的内容,触发器就应该起作用。

I have a Maximo database that has a table structure I cannot change. I am looking to copy the primary email address into the PERSON table anytime it is created or updated. The following structure describes the PERSON table and 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 on the PERSONID column. Here is what I would like the trigger to do:
If the EMAIL table is updated or a new row is inserted, I would like to copy the EMAILADDRESS field to the corresponding entry (as linked by PERSONID) on the PERSON table IF the ISPRIMARY field is equal to 1 (1 means primary, 0 means secondary).
I have not written many triggers so I want to make sure I am only looking at the rows that are being updated or inserted into the EMAIL table and only updating the PERSON table if there is a new/updated primary email address. Thanks in advance for all of your help!


UPDATE 1:
After looking at Cade's response, here is the trigger I am beginning to form:

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  

I believe that trigger should work anytime something is updated OR inserted to the email table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-10-17 19:15:34

问题与 EMAIL 表上缺少主键有关。触发器最适合与不可变的主键配合使用。

另外,如果一行更改为非主要行,您是否从 PERSON 中删除该条目?

因此,问题领域中似乎仍然存在一些悬而未决的问题,但这里是触发器的样子。您可以添加一些内容来查找实际发生更改的行 - 但要小心 NULL。

CREATE TRIGGER EMAIL_update ON UPDATE
AS
BEGIN
    UPDATE PERSON
    SET EMAIL = i.EMAILADDRESS    
    FROM PERSON
    INNER JOIN inserted AS i
        ON i.PERSONID = PERSON.PERSONID
    INNER JOIN deleted AS d -- could try changing this to a left join and use same trigger for INSERT
        ON -- what? could use PERSONID, but it's not unique
    WHERE i.ISPRIMARY = 1 -- This helps with uniqueness, but what about things leaving primary?
    -- AND i.EMAILADDRESS <> PERSON.EMAIL -- Could add this (what about NULLs?)
END

Problem is going to be related to the lack of a primary key on the EMAIL table. Triggers work best with immutable primary keys.

Also, what if a row changes to not be primary, do you remove the entry from PERSON?

So still seems like some open questions in the problem domain, but here's a stab at what the trigger would look like. You could add some things which look for rows where a change is actually occurring - but be careful about NULLs.

CREATE TRIGGER EMAIL_update ON UPDATE
AS
BEGIN
    UPDATE PERSON
    SET EMAIL = i.EMAILADDRESS    
    FROM PERSON
    INNER JOIN inserted AS i
        ON i.PERSONID = PERSON.PERSONID
    INNER JOIN deleted AS d -- could try changing this to a left join and use same trigger for INSERT
        ON -- what? could use PERSONID, but it's not unique
    WHERE i.ISPRIMARY = 1 -- This helps with uniqueness, but what about things leaving primary?
    -- AND i.EMAILADDRESS <> PERSON.EMAIL -- Could add this (what about NULLs?)
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文