Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The multi-part identifier "inserted.Id" could not be bound while creating trigger

Tags:

sql-server

I am trying to create a trigger on a table in SQL Server 2012, but it is giving an error like below

"The multi-part identifier "inserted.Id" could not be bound",

The query I am executing is

CREATE TRIGGER dbo.[TR_t_documents_InsertUpdateDelete] ON 

dbo.[t_documents] AFTER INSERT, UPDATE, DELETE
AS
BEGIN 

    UPDATE dbo.[t_documents]
    SET dbo.[t_documents].[UpdatedAt] = CONVERT(DATETIMEOFFSET, SYSUTCDATETIME()) 
    FROM
        INSERTED
    WHERE inserted.[Id] = dbo.[t_documents].[Id]
END

The same is executing successfully in SQL Server 2014.

Can anyone help me why this is happening in SQL server 2012?

like image 473
narendramacha Avatar asked Jul 16 '15 13:07

narendramacha


1 Answers

This is due to the collation you have for the database. In this case, you are using a case sensitive collation, so the table names need to be consistently. For the virtual trigger tables, these need to be in upper case, for example:

CREATE TRIGGER dbo.[TR_t_documents_InsertUpdateDelete] ON 

dbo.[t_documents] AFTER INSERT, UPDATE, DELETE
AS
BEGIN 

    UPDATE dbo.[t_documents]
    SET dbo.[t_documents].[UpdatedAt] = CONVERT(DATETIMEOFFSET, SYSUTCDATETIME()) 
    FROM
        INSERTED
    WHERE INSERTED.[Id] = dbo.[t_documents].[Id]
    --    ^^^^^^^^
    --      THIS!
END
like image 175
DavidG Avatar answered Nov 15 '22 04:11

DavidG