http://www.cnblogs.com/gaizai/p/3358998.html
--查询Default Trace是否开启
SELECT * FROM sys.configurations WHERE configuration_id = 1568;
--开启Default Trace
sp_configure 'show advanced options' , 1 ;GO
--获取当前跟踪文件的路径
SELECT * FROM ::fn_trace_getinfo(0)
--创建测试数据库
USE MASTER
GO
CREATE DATABASE TraceDB
--通过创建表产生一个DDL事件
USE TraceDB
GO
CREATE TABLE dbo.Trace_log(
Id INT IDENTITY(1,1) not null,
Sometext CHAR(3) null
)
--Script1:返回刚刚Create操作的信息
-- =============================================
-- Author: <听风吹雨>
-- Create date: <2013.05.03>
-- Description: <读取、过滤log.trc文件>
-- Blog: <http://www.cnblogs.com/gaizai/>
-- =============================================
DECLARE @tracefile NVARCHAR(MAX)
SET @tracefile = (SELECT LEFT([path],LEN([path])-CHARINDEX('\',REVERSE([path])))+ '\log.trc' FROM sys.traces WHERE [is_default] = 1)
SELECT TOP 100
gt.[HostName]
,gt.[ServerName]
,gt.[DatabaseName]
,gt.[SPID]
,gt.[ObjectName]
,gt.[objecttype] [ObjectTypeID]
,sv.[subclass_name] [ObjectType]
,e.[category_id] [CategoryID]
,c.[Name] [Category]
,gt.[EventClass] [EventID]
,e.[Name] [EventName]
,gt.[LoginName]
,gt.[ApplicationName]
,gt.[StartTime]
,gt.[TextData]
FROM fn_trace_gettable(@tracefile, DEFAULT) gt
LEFT JOIN sys.trace_subclass_values sv ON gt.[eventclass] = sv.[trace_event_id] AND sv.[subclass_value] = gt.[objecttype]
INNER JOIN sys.trace_events e ON gt.[eventclass] = e.[trace_event_id]
INNER JOIN sys.trace_categories c ON e.[category_id] = c.[category_id]
WHERE gt.[spid] > 50 AND --50以内的spid为系统使用
gt.[DatabaseName] = 'TraceDB' AND --根据DatabaseName过滤
gt.[ObjectName] = 'Trace_log' AND --根据objectname过滤
e.[category_id] = 5 AND --category 5表示对象,8表示安全
e.[trace_event_id] = 46 --trace_event_id 46表示Create对象(Object:Created),47表示Drop对象(Object:Deleted),93表示日志文件自动增长(Log File Auto Grow),164表示Alter对象(Object:Altered),20表示错误日志(Audit Login Failed)
ORDER BY [StartTime] DESC