I have an issue with this AFTER INSERT trigger that I'm using for auditing:
Cannot insert the value NULL into column 'TermID', table 'AuditTerms'; column does not allow nulls. INSERT fails.
TermKeys table definition:
TermID int Identity PK
Key int
Action int
AuditTerms table definition:
TermID int PK
UpdatedAt datetime
Trigger definition:
ALTER TRIGGER [dbo].[TRG_TermKeys_AuditTerms_IUD] ON [dbo].[TermKeys]
AFTER INSERT, UPDATE, DELETE NOT FOR REPLICATION AS
BEGIN
SET NOCOUNT ON;
DECLARE @termid int;
IF EXISTS(SELECT * FROM Deleted) SELECT @termid = TermID FROM Deleted;
IF EXISTS(SELECT * FROM Inserted) SELECT @termid = TermID FROM Inserted;
IF EXISTS(SELECT TermID FROM AuditTerms WHERE (TermID = @termid))
BEGIN
UPDATE AuditTerms SET UpdatedAt = getdate() WHERE (TermID = @termid);
END
ELSE
BEGIN
INSERT INTO AuditTerms (TermID, UpdatedAt) VALUES (@termid, getdate());
END;
END;
TermID is an Identity field and the PK, does the Inserted table contain the Identity value at this point?
Should I be using @@Identity or some other way?
INSERT INTO AuditTerms (TermID, UpdatedAt) SELECT TermId, GetDate() FROM Inserted
. If that doesn't help post more code.