我的老板昨天询问了一位客户询问他们如何找出谁删除了他们的sql Server数据库中的一些数据(如果重要的话,这是快递版).
我认为这可以从事务日志中找到(假设它没有被截断) – 这是正确的吗?如果是这样,您如何才能真正找到这些信息呢?
解决方法
我没有尝试过Express上的fn_dblog,但如果它可用,下面的内容将为您提供删除操作:
SELECT * FROM fn_dblog(NULL,NULL) WHERE Operation = 'LOP_DELETE_ROWS'
获取您感兴趣的交易的交易ID,并识别发起交易的SID:
SELECT [Transaction SID] FROM fn_dblog(NULL,NULL) WHERE [Transaction ID] = @TranID AND [Operation] = 'LOP_BEGIN_XACT'
然后从SID中识别用户:
SELECT * FROM sysusers WHERE [sid] = @SID
DECLARE @TableName sysname SET @TableName = 'dbo.Table_1' SELECT u.[name] AS UserName,l.[Begin Time] AS TransactionStartTime FROM fn_dblog(NULL,NULL) l INNER JOIN ( SELECT [Transaction ID] FROM fn_dblog(NULL,NULL) WHERE AllocUnitName LIKE @TableName + '%' AND Operation = 'LOP_DELETE_ROWS' ) deletes ON deletes.[Transaction ID] = l.[Transaction ID] INNER JOIN sysusers u ON u.[sid] = l.[Transaction SID]