How to Track Database Transaction Log in SQL Server 2012

SQL Server database transaction logs are the most important part of a database. Every DBA and database developer should understand Database Transaction logs, this is a huge topic of SQL Server. 
 
About database Transaction logs
 
SQL Server databases have a transaction log that records all transactions and the database modifications made by each transaction. This contains enough information to undo all changes made to the data file as part of any separate transaction. If you want to see the data in a given database's transaction log, there is an undocumented SQL Server command called DBCC LOG. This command is used to view the transaction log for a specific database. The DBCC Log command only shows log information and this command will not provide you detailed information. 
 
Syntax
  1. DBCC log ({dbid|dbname}, [, type= {-1|0|1|2|3|4}]) 
Parameters
 
Dbid or dbname: Enter either the dbid or the database name of the database in question.
 
Type is the type of output as in the following:
  1. minimum information (operation, context, transaction id)
  2. More information (plus flags, tags, row length, description)
  3. Very detailed information (plus object name, index name, page id, slot id)
  4. Full information about each operation
  5. Full information about each operation plus hexadecimal dump of the current transaction log's row.
1. The full information about each operation plus hexadecimal dump of the current transaction log's row, plus Checkpoint Begin, DB Version, Max XDESID by default type = 0.
 
Note: DBCC LOG can also be called as a system function for use in a SELECT statement or other queries via the fn_dblog function.
 
Syntax
  1. USE MASTER  
  2.   
  3. GO  
  4.   
  5. SELECT TOP 10 * FROM fn_dblog(<start>, <end>)  
The fn_dblog () function accepts two parameters.
 
The first is the starting log sequence number, or LSN. You can also specify NULL, which means it will return everything from the start of the log.
 
The second is the ending LSN. You can also specify NULL, which means you want to return everything to the end of the log .
 
I will create a small Test Environment and how to work with SQL Server Transaction logs using DDL and DML oprations, like a create, insert, update and delete.
 
The test environment
 
Create the test environment with the following:
  1. --Create Test DB.    
  2. USE [master];    
  3. GO    
  4. CREATE DATABASE TrackingDBLog;    
  5. GO    
  6. -- Create tables.    
  7. USE TrackingDBLog;    
  8. GO    
  9. CREATE TABLE [Test] (    
  10. [ID] INT IDENTITY (1, 1),    
  11. [Date] DATETIME DEFAULT GETDATE (),    
  12. [NameCHAR (25) DEFAULT 'A');  
Now you can check all information and processes that have been used by SQL Server to create the database and table. We will run the following code to check the log file for this newly created database to check the processes and steps of SQL Server.
  1. DBCC log (TrackingDBLog,1)    
  2. GO    
  3. select Top 10 * FROM fn_dblog(null,null)    
  4. USE TrackingDBLog;    
  5. GO    
  6. select COUNT(*) from fn_dblog(null,null)   
 
 
We have created a dummy database and a blank table. You can check the logs by using this function to get the details for all the processes used to create the database and table. Look at the following code to see the data in the transaction log file.
 
The DDL Scenario
 
Create the DDL Scenario with the following:
  1. USE TrackingDBLog;    
  2. GO    
  3. select [Current LSN],    
  4. [Operation],    
  5. [Transaction Name],    
  6. [Transaction ID],    
  7. [Transaction SID],    
  8. [SPID],    
  9. [Begin Time]    
  10. FROM fn_dblog(null,null) 
 
 
 
DML Scenario
 
Create the DML Scenario with the following.
 
I will now run a little DML script to check how data insertion, updates or deletion is logged in the database log file. During this operation you can also track how a page is allocated or de-allocated.
  1. USE TrackingDBLog    
  2. GO    
  3. INSERT INTO [Test] DEFAULT VALUES ;    
  4. GO 15    
  5. SELECT TOP 6 * FROM [Test]    
  6. SELECT count (*) TotalCount FROM [Test]    
  7. GO    
  8. UPDATE [Test]    
  9. SET [Name]='B'    
  10. WHERE [ID] >3    
  11. And [ID] <=6    
  12. GO    
  13. SELECT TOP 6 * FROM [Test]    
  14. SELECT count(*) TotalCount FROM [Test]    
  15. GO    
  16. DELETE [Test]    
  17. WHERE [ID]>6    
  18. GO    
  19. SELECT * FROM [Test]    
  20. SELECT count (*) TotalCount FROM [Test  
This is the output of the preceding query.
 
 
Let's check our database log file again using the following script:
  1. USE TrackingDBLog;    
  2. GO    
  3. select [Current LSN],    
  4. [Operation],    
  5. [Transaction Name],    
  6. [Transaction ID],    
  7. [Transaction SID],    
  8. [SPID],    
  9. [Begin Time]    
  10. FROM fn_dblog(null,null) 
 
 
How a backup relates with the database transaction log
 
Now to run a backup and see the transaction log file again. Run a backup on this database and then again check the transaction log file.
  1. SELECT * FROM fn_dblog(null,null)    
  2. GO    
  3. BACKUP DATABASE TrackingDBLog TO DISK = 'D:\backup\TrackingDBLog.bak'    
  4. GO    
  5. SELECT COUNT(*) FROM fn_dblog(null,null) 
 
 
Let's check our database log file again using the preceding script.
 
 
 
In the next article I will explain:
  • How to find the user who DROPed or DELETEd statements on your SQL Server Objects.
  • How to recover deleted data and tables on SQL Server using the Transaction Log.


Similar Articles