>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

http://rcm.amazon.com/e/cm?t=co04b8-20&o=1&p=8&l=bpl&asins=B000071HPQ&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=FFFFFF&bg1=FFFFFF&f=ifrhttp://rcm.amazon.com/e/cm?t=co04b8-20&o=1&p=8&l=bpl&asins=B004U9USEA&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=FFFFFF&bg1=FFFFFF&f=ifrhttp://rcm.amazon.com/e/cm?t=co04b8-20&o=1&p=8&l=bpl&asins=1935182048&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=FFFFFF&bg1=FFFFFF&f=ifr

Advertisements

Posted on April 26, 2011, in Sql Server Management, Traces and Logs. Bookmark the permalink. 1 Comment.

  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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: