I am trying to call an IF statement on my trigger so it won’t archive expired files. (I only want to keep files that have been deleted but have not been expired)
My error is The multi-part identifier “d.ExpiryDate” could not be bound.
My Code:
ALTER TRIGGER [dbo].[ArchiveDB]
ON [dbo].[TBL_Content]
AFTER DELETE
AS
BEGIN
declare @ContentID int
set @ContentID = (select ContentID from deleted)
IF (d.ExpiryDate > getDate() )
begin
insert into ArchiveBackup.dbo.TBL_Deleted_Content
(ContentID, StartDate, ExpiryDate, Title... etc)
select
d.ContentID,d.StartDate,d.ExpiryDate,d.Title... etc
from deleted as d
end
END
Thanks for the help!
You’d have to tell SQL Server where to find the ExpiryDate, like:
Be aware that a trigger can be called for cases when multiple rows where deleted. It might be better to replace the entire
ifcontruct with a query:Or even better, write a stored procedure that deletes rows instead of deleting them directly from the table. Stored procedures are way easier to understand and maintain than triggers.