15

我正在尝试创建一个触发器,以在更改存储库时更新联系人信息。

CREATE TRIGGER contacts_f_tr
ON contacts_f
AFTER UPDATE
AS
BEGIN
  ---
  ---Update repository data
  ---
  IF UPDATE (mail)
    BEGIN
      UPDATE mails
      SET contact = inserted.name, mail = inserted.mail
      WHERE mails.idcontact IN (SELECT mail FROM deleted) AND mails.tablecontact = 2
    END
END

我对此很陌生,我得到了这个错误:

The multi-part identifier "INSERTED.name" could not be bound.
The multi-part identifier "INSERTED.mail" could not be bound.
4

1 回答 1

24

FROM Inserted在你的UPDATE陈述中遗漏了 - 试试这个:

CREATE TRIGGER contacts_f_tr
ON contacts_f
AFTER UPDATE
AS
BEGIN
  ---
  ---Update repository data
  ---
  IF UPDATE (mail)
    BEGIN
      UPDATE mails
      SET contact = inserted.name, mail = inserted.mail
      FROM Inserted     <<==== add this line here!
      WHERE mails.idcontact IN (SELECT mail FROM deleted) AND mails.tablecontact = 2
    END
END

另外 - 一旦你包含了那个伪表,你应该以某种方式引用它/将它连接到其他东西......

更新:WHERE好吧,如果您将伪表添加到等式中,您可能需要添加一些额外的子句Inserted- 这完全取决于您的要求,我不知道 - 但它可能类似于

WHERE mails.idcontact IN (SELECT mail FROM deleted) 
  AND mails.tablecontact = 2
  AND mails.MailId = Inserted.MailId

或类似的东西(以避免在您的UPDATE声明中使用笛卡尔积)。

于 2013-01-07T19:55:58.803 回答