如何从 SQL Server 的事务日志文件中获取用户?
我试过这个查询,但它没有给我正确的用户
SELECT
CASE
WHEN (SELECT NAME
FROM SYS.SERVER_PRINCIPALS lgn
WHERE Rtrim(lgn.SID) = Rtrim(log2.[TRANSACTION SID])) IS NOT
NULL THEN
'login: '
+ Upper((SELECT NAME FROM SYS.SERVER_PRINCIPALS lgn WHERE Rtrim(lgn.SID)
= Rtrim
(log2.[TRANSACTION SID])))
ELSE 'db user: '
+ Upper((SELECT NAME FROM SYS.SERVER_PRINCIPALS lgn WHERE Rtrim(
lgn.SID)
= Rtrim(log2.[TRANSACTION SID] )))
END
AS 'Login_or_User',
log1.*,
CASE log1.__$OPERATION
WHEN 1 THEN 'DELETE'
WHEN 2 THEN 'INSERT'
WHEN 4 THEN 'UPDATED VALUE'
WHEN 3 THEN 'PREVIOUS UPDATED VALUE'
ELSE NULL
END
AS operation,
CONVERT(VARCHAR(19), log2.[BEGIN TIME]) AS 'Begin Time',
LEFT(log.ALLOCUNITNAME, Charindex('.', log.ALLOCUNITNAME) - 1)
AS 'Schema',
LEFT(RIGHT(log.ALLOCUNITNAME, Len(log.ALLOCUNITNAME) -
Charindex('.', log.ALLOCUNITNAME)),
Charindex('.', RIGHT(log.ALLOCUNITNAME,
Len(log.ALLOCUNITNAME)
-
Charindex('.', log.ALLOCUNITNAME))) - 1) AS 'Object',
SYS.Fn_cdc_hexstrtobin(Substring(log.[CURRENT LSN], 1, 8)
+ Substring(log.[CURRENT LSN], 10, 8)
+
Substring(log.[CURRENT LSN], 19, 4)) currentlsn
FROM DBO.TEMPTABLE log1
LEFT JOIN ::FN_DBLOG(NULL, NULL) log
ON log1.__$SEQVAL = SYS.Fn_cdc_hexstrtobin(
Substring(log.[CURRENT LSN], 1, 8)
+ Substring(log.[CURRENT LSN], 10, 8)
+ Substring(log.[CURRENT LSN], 19, 4))
LEFT JOIN ::FN_DBLOG(NULL, NULL)log2
ON log.[TRANSACTION ID] = log2.[TRANSACTION ID]
LEFT JOIN SYS.ALLOCATION_UNITS au
ON log.ALLOCUNITID = au.ALLOCATION_UNIT_ID
LEFT JOIN SYS.PARTITIONS p
ON p.PARTITION_ID = au.CONTAINER_ID
LEFT JOIN SYS.OBJECTS so
ON so.OBJECT_ID = p.OBJECT_ID
LEFT JOIN SYS.SERVER_PRINCIPALS spa
ON spa.SID = log2.[TRANSACTION ID]
此代码用于检查已启用 CDC(更改数据捕获)的数据库,然后选择特定表以获取其更改