2 errors: an identifier with several parts "inserted .name" cannot be associated

I am trying to create a trigger to update contact information in the repository when it was changed.

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 

I am completely new at this, and I have errors like this:

 The multi-part identifier "INSERTED.name" could not be bound. The multi-part identifier "INSERTED.mail" could not be bound. 
+8
sql sql-server-2008 triggers
source share
1 answer

You are missing FROM Inserted in an UPDATE - try the following:

 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 

Also - after including this pseudo-table, you should somehow refer to it / join something else ...

Update: you may need to add an additional WHERE if you add the Inserted pseudo-table to the equation - which exactly depends on your requirements, which I don’t know, but it could be something like

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

or something like that (to avoid the Cartesian product in your UPDATE ).

+15
source share

All Articles