Tuesday, April 26, 2011

SQL Server: How to Read Trace File to Detect Database Detachment


On of our production server contains more then 200 databases. Few of them are rarely used but still required. Few days back, someone from DBA’s  team accidentally detached on of less used database. But once we need it we got error as there was no required database on server.
To find out, that when and who detached this database we have quickly executed a simple script.
First get current trace file name from sys.traces table
SELECT * FROM sys.traces
GO
Then copy trace file name and assign it @trace_file parameter and execute following script.

DECLARE @trace_file NVARCHAR(500)
SELECT  @trace_file = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Log\log_88.trc'
SELECT  *
FROM    [fn_trace_gettable](@trace_file, DEFAULT)
WHERE   TextData LIKE '%DETACH%'
ORDER BY starttime DESC

3 comments:

  1. So, does it mean that sys.traces table always logs the changes and we simply execute a select statement there? Or should we create a new trace file for future changes? Thanks.

    ReplyDelete
  2. Thank you so much ...it's work for me..

    ReplyDelete
  3. Thanks you so much.. it's work.

    ReplyDelete

All suggestions are welcome