You can use triggers to keep track of data changes on a database table
You can find an example of how to log sql table data changes using triggers at referred tutorial
Here you need to know, if the users of your applications are mapped to a single user on database, you will have to pass insertedby, updatedby or deletedby user information explicitely in your UPDATE,DELETE or INSERT commands as table field values. Otherwise, what you will get will be the same user for all transactions
Just like temporal tables on SQL Server 2016, you are required to use or create your history table or log table manually
Please check following SQL trigger code
CREATE TRIGGER dbo.LogTbl
ON dbo.Tbl
AFTER INSERT, UPDATE, DELETE
AS
IF EXISTS (
SELECT * FROM Inserted
) -- INSERT or UPDATE
BEGIN
IF EXISTS (
SELECT * FROM Deleted
)
-- UPDATE Statement was executed
INSERT INTO HistoryTbl (
...
UpdatedDate,
UpdatedByUserId
)
SELECT
...
i.UpdatedDate,
i.UpdatedByUserId
FROM Deleted d
INNER JOIN Inserted i ON i.Id = d.Id
ELSE
-- INSERT Statement was executed
INSERT INTO HistoryTbl (
...
InsertedDate,
InsertedByUserId
)
SELECT
...
i.InsertedDate,
i.InsertedByUserId
FROM Inserted i
END
ELSE
-- DELETE Statement was executed
INSERT INTO HistoryTbl (
...
DeletedDate,
DeletedByUserId
)
SELECT
GETDATE(), ---!!!
USER_ID() ---!!!
FROM Deleted
GO
Pay attention to DELETE command, in delete command we don't have deletedby user data. So I used USER_ID(), you can use the USER_NAME() too
An other approach can be, instead of DELETE'ing a row, you can DELETE it by UPDATE'ing an ACTIVE column value to false. I mean UPDATE data, it you set its active field to 0, this can be thought as it is deleted.
So you have to modify all your codes by selecting only active data...