I would like to track all changes for specific tables. So I want to have a trigger to fire when table get updated, for each updated field I want to insert a row in my audit log table with the column old value and new value.
For example lets say I have a employee table like this
CREATE TABLE [dbo].[Employee]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Current] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
[Active] [bit] NOT NULL
)
And a log table like this
CREATE TABLE [dbo].[Logs]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime] NOT NULL,
[Puser] [nchar](10) NULL,
[WinUser] [nchar](20) NULL,
[Computer] [nchar](20) NULL,
[Type] [nchar](20) NULL,
[ObjectType] [nchar](20) NULL,
[ObjectName] [nchar](20) NULL,
[Action] [nchar](20) NULL,
[RecordID] [nchar](20) NULL,
[FIeldName] [nchar](20) NULL,
[OldValue] [nvarchar](50) NULL,
[NewValue] [nvarchar](50) NULL,
[ParentName] [varchar](20) NULL,
[ParentID] [varchar](20) NULL,
[RowDump] [nvarchar](max) NULL
)
And I change the password and active column for an employee, I would like to enter 2 rows into the Logs
table (including the employee id in recordid, column name in fieldname, oldvalue, newvalue)
Thanks in advance
1条答案
按热度按时间sg2wtvxw1#
The best method to do this kind of thing is to use existing tools in SQL Server, such as Change Data Capture , and SQL Auditing .
However, if you really want to roll your own logging, the following should get you started.
It's unclear what some of your logging columns are supposed to represent so I've omitted them. It also seems like overkill.
If the column types are mixed then you should cast at least the first value in the
CROSS APPLY (VALUES
tosql_variant
.db<>fiddle
I note that some of your data types are questionable, such as nullable columns, lengths, and object and username columns should be
sysname
. Plain-text passwords are also a bad idea.