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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With