Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Presenting for PASSMN May 17thSQL Server DBA Tip 8 - Server Monitoring - Baseline »
    comments

    The Default Trace exists in SQL Server 2005 and higher versions, in all editions.  The trace has been questioned: what is it, and why is it helpful?  We will go over the answers to these questions and look into the Default Trace, showing how to use it to retrieve information it collects while enabled.

    What is the Default Trace?

    The Default Trace is a trace prepackaged into SQL Server and enabled by default when you install SQL Server.  You cannot alter the events the Default Trace captures but can disable the trace by executing the following.

    The Default Trace’s value may provide a reason to leave the trace enabled.  The code below is provided to show of how to disable it is provided in case the need arises for it to be disabled.

     

    1. sp_configure 'show advanced options', 1
    2. GO
    3. RECONFIGURE
    4. GO
    5. sp_configure 'default trace enabled', 0
    6. GO
    7. RECONFIGURE
    8. GO

     

    The Default Trace tracks events that revolve around configurations changes and collects information on changes such as ALTER object events and login failures.  Since the trace is not documented thoroughly in Books Online, we need to execute a query to determine the events that are being captured.  This is done by using fn_trace_geteventinfo, fn_trace_getinfo, sys.trace_events and sys.trace_columns as shown below.

     

    1. SELECT
    2.     TraceEvents.name as [Event],
    3.     TraceData.name as [Data Collected]
    4. FROM ::fn_trace_geteventinfo((SELECT TOP 1 TraceID FROM ::fn_trace_getinfo(default))) TraceInfo
    5. JOIN sys.trace_events TraceEvents ON TraceInfo.eventID = TraceEvents.trace_event_id
    6. JOIN sys.trace_columns TraceData ON TraceInfo.columnid = TraceData.trace_column_id

     

    The results of the unique event names

     

    As with other traces, not all columns have available data pertaining to the specific event.  If a value is NULL, it is likely that the information is not available for the captured event.  This can be verified by using the query shown above, by checking the Data Collected values returned.

     

    Default Trace in action – Why is it helpful?

     

    When working in a DBA Team, it can be important to know when something was changed and who made the change, in SQL Server.  This information is used for auditing and troubleshooting. 

     

    Here we will test the capture of TABLE CREATE, ALTER and DROP running the following with the default trace enabled.

     

    1. CREATE TABLE Junked (COL INT)
    2. GO
    3. ALTER TABLE Junked
    4.  ADD COL2 VARCHAR(10)
    5. GO
    6. DROP TABLE Junked
    7. GO

     

    To query the event that was forced above, we can use the fn_trace_gettable to load the trace file into a table format.  The trace file that is current can be found by running the fn_trace_getinfo.

     

    1. SELECT * FROM ::fn_trace_getinfo(default)

     

    Load the file that was represented in the property value 2 row returned.

     

    Then use the file path as:

     

    1. SELECT
    2.     TraceInfo.ServerName,
    3.     TraceInfo.StartTime,
    4.     TraceInfo.LoginName,
    5.     TraceInfo.ObjectName,
    6.     TraceEvents.name
    7. FROM
    8. ::fn_trace_gettable('C:\SQL2008R2\MSSQL10_50.TK2008R2\MSSQL\Log\log_257.trc',0) TraceInfo
    9. JOIN sys.trace_events TraceEvents ON TraceInfo.eventclass = TraceEvents.trace_event_id
    10. WHERE ObjectName = 'Junked'

     

    From the illustration above, we can see that the table “Junked” was created, altered and then deleted.  This information can be invaluable in the event a table was dropped or altered, in the event that change caused a severe problem to the functionality of the table.  The time that the table was changed can show a specific set of events that may have led up to it, or reasoning they occurred.  Further value is seen in audit reporting on production systems that require change control. 

     

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt
    Social SitingsTwitterLinkedInLTD RSS Feed
    1412 views
    Instapaper

    2 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis I like to call it the Black Box of SQL Server
    05/06/11 @ 06:57
    Comment from: SQLArcher [Member] Email
    SQLArcher Thanks for the tip, interesting read.
    05/07/11 @ 00:03

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)