我想检查哪个用户在MS SQL Server 2008的特定表中执行了插入操作。我知道一些日志记录信息已存储,但是我不知道如何访问它。如果能提供有关我的问题的具体信息,以及如果我也需要其他信息的话,我将不胜感激。
谢谢!
您是在谈论从事务日志中检索此消息吗?
这是不理想的,因为您不能保证相关行仍将在活动日志中可用,并且查询效率较低,但是可以通过以下操作(返回USER_NAME()而不是可能有用的方式SUSER_NAME())进行。
USER_NAME()
SUSER_NAME()
更改dbo.X为您的实际表名。
dbo.X
DECLARE @allocation_unit_ids TABLE ( allocation_unit_id BIGINT PRIMARY KEY ) INSERT INTO @allocation_unit_ids SELECT allocation_unit_id FROM sys.allocation_units au JOIN sys.partitions p ON au.container_id = CASE WHEN au.type IN ( 1, 3 ) THEN p.hobt_id WHEN au.type = 2 THEN p.partition_id END WHERE p.object_id = OBJECT_ID('dbo.X'); WITH L1 AS (SELECT [Transaction ID], [Begin Time], [End Time], [Transaction SID], CASE WHEN Operation = 'LOP_INSERT_ROWS' AND AllocUnitId IN (SELECT allocation_unit_id FROM @allocation_unit_ids) THEN 1 END AS I FROM sys.fn_dblog(NULL, NULL) l), L2([Transaction ID], TransactionBegin, TransactionEnd, sid) AS (SELECT [Transaction ID], MAX([Begin Time]), MAX([End Time]), MAX([Transaction SID]) FROM L1 GROUP BY [Transaction ID] HAVING 1 = MAX(I)) SELECT TransactionBegin, TransactionEnd, p.name AS PrincipalName FROM L2 JOIN sys.database_principals p ON p.sid = L2.sid