I have the following SQL Server trigger that I want to do the following:
- On
INSERT, save the new row inDocumentsintoArchivedDocuments - On
UPDATE, save the new row revision ifWordCountorPageCountorContributorCounthave changed from the current values with theUPDATE
How do I achieve this?
My current trigger just saves to ArchivedDocument after each INSERT or UPDATE.
EDIT: Id is the primary key for the document table. DocumentId is a foreign key that references Document.Id.
CREATE TRIGGER [TRG__Documents]
ON [Documents]
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @now datetimeoffset = sysdatetimeoffset();
INSERT INTO [DocumentRevisions] ([DocumentId], [Time], [WordCount], [PageCount], [ContributorCount])
SELECT
[inserted].[Id],
@now,
[inserted].[WordCount],
[inserted].[PageCount],
[inserted].[ContributorCount]
FROM
[inserted]
END