SQL Server row date last modified

前端 未结 6 567
说谎
说谎 2021-01-07 16:09

A colleague of mine says that SQL Server saves the date and time of last modification in a \"hidden column\" in each record. I am pretty sure that he said something wrong. C

6条回答
  •  既然无缘
    2021-01-07 16:46

    You can try to read SQL Server transaction log using PageID.

    It's not a guaranteed way, but it works in many cases , and a little more accurate than querying index statistics

    /* This is example how to query datetime of data changes using SQL Server 
       transaction log, if audit routine wasn't set */
    
    USE [AdventureWorksDW2017]
    GO
    
    UPDATE dbo.FactFinance
    SET Date = GETDATE(), Amount = Amount + 1
    WHERE FinanceKey = 1
    GO
    
    WITH main AS
    (   
        SELECT 
            replace(replace(sys.fn_PhysLocFormatter (%%physloc%%), ')', ''), '(', '') page,
            FinanceKey, Date, Amount
        FROM 
            dbo.FactFinance (NOLOCK)   
        WHERE 
            FinanceKey = 1
    ),
    tlog AS
    ( 
        SELECT 
            page, l2.[Begin Time], L2.[End Time],
            l1.AllocUnitName l1_uname, l2.[Transaction Name] l2_tname,
            l1.[Transaction ID]
        FROM 
            main
        JOIN
            sys.fn_dblog(NULL, NULL) l1 ON PATINDEX('%'+main.page+'%', l1.[Lock Information]) > 0
        LEFT JOIN 
            sys.fn_dblog(NULL, NULL) l2 ON l2.[Transaction ID] = l1.[Transaction ID]  
    )
    SELECT 
        PAGE, [Transaction ID], 
        MIN([Begin Time]), MIN([End Time]),
        MIN(l1_uname), MIN(l2_tname)
    FROM
        tlog
    GROUP BY 
        [Transaction ID], page
    

提交回复
热议问题