how Reading the transaction log in SQL Server

Ozra Hasannejad 21 Reputation points
2022-12-17T10:18:58.037+00:00

Some data is missing in our warehouse. To check the reason, we need to check the log for that time. We will be very happy if you help us in this field.

SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Ronen Ariely 15,206 Reputation points
    2022-12-17T10:55:04.293+00:00

    Hi,

    The transaction log file is not intended for direct reading by users but for internal use. What you are asking is a level 500 actions (internals) and it is not officially supported.

    With that said, it can be done using an undocumented functions/commands like DBCC log, fn_dblog(), fn_dump_dblog(), fn_full_dblog()

    You can find several posts in my blog about using these functions.

    If you are registered to the PASS Summit then I highly recommend to watch the recording to my session. This was exactly the topic I spoke about.

    You can see the promo of the session here: https://www.facebook.com/ronen.ariely/videos/793222385150305

    To watch the recording and download all the demo code you can follow this:

    (1) Check the list of speakers speakers: https://passdatacommunitysummit.com/speakers/all-speakers
    (2) Search: Ronen
    (3) Click the speaker name to see the session information

    • I think that only people that were registered to the vent can download it.

    ----------

    14150-image.pngRonen Ariely
    Personal Site | Blog | Facebook | Linkedin

    0 comments No comments

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-12-17T11:07:34.893+00:00

    There is no documented way to use the transaction log for this.. There are however two undocumented functions available:

    sys.fn_dblog which reads from the active portion of the transaction log.
    sys.fn_dump_dblog which reads from the transaction log backups.

    Assuming that you backup the transaction log every 15 minutes or so, it's the latter one you need to use.

    None of these are easy to use and you need to have the time and patience to decipher the information.

    I am not going to give any examples here, because I have not used them very much myself. Nor will I give any links, because you can use Google just as well as I can. Except that you should read https://www.sqlskills.com/blogs/paul/using-fn_dblog-fn_dump_dblog-and-restoring-with-stopbeforemark-to-an-lsn/, because there is an important caveat about sys.fn_dump_dblog.

    There are also commercial tools available, or at least one: https://www.apexsql.com/sql-tools-log/. I have not used it myself, and I cannot vouch for it. (There have been others in the past, but I don't know if they are still in business.)

    If all you want to do is to restore the data, restore an old backup of the database, and apply transaction logs WITH STANDBY to get read-only access of the database at that point in time, but you can still apply more log backups.


  3. Seeya Xi-MSFT 16,586 Reputation points
    2022-12-19T03:30:25.977+00:00

    Hi @Ozra Hasannejad ,

    I've used SQL Apex before and it's really a great program. The main advantage of it is that any operation can be seen visually.
    Also, you can refer to this blog:
    https://www.sqlshack.com/how-to-read-a-sql-server-transaction-log/

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Peter Groft 106 Reputation points
    2022-12-20T06:39:08.043+00:00
    1. Right-click SQL Server Logs, point to View, and then click either SQL Server Log or SQL Server and Windows Log.
    2. Expand SQL Server Logs, right-click any log file, and then click View SQL Server Log. You can also double-click any log file.

    Hope This Works,
    Peter


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.