|
mfemenel
Professor Frink
1421 Posts |
Posted - 2005-04-27 : 08:46:53
|
| I've included some code I used a while ago below. I use 1 job to start the trace and pass it the file name I want it to store the results in. You can create the Sp and kick it off with a job.The second piece is an sp I used to stop the job which would run x minutes after the start job so you can shut the trace down. In my situation, I do a sample once per hour for 10 mins and suck the file into a table. --Code to start a trace from spCREATE procedure trace_start@filename varchar(8000)as--Set the time for the trace to stopdeclare @dt_end datetimeselect @dt_end = convert(varchar(30), getdate(), 101)+' 23:59:00'--Create an output variable for the id of the traceDECLARE @TraceID intEXEC CreateTrace @filename, @OutputTraceID = @TraceID OUT, @OverwriteFile=1, @StopAt=@dt_endEXEC AddEvent @TraceID, 'SP:Completed', 'TextData, Duration,StartTime,EndTime,ApplicationName,DatabaseName,DatabaseID,NTUserName,SPID,SQLSecurityLoginName,CPU,READS,WRITES,'EXEC AddFilter @TraceID, 'Duration', 0,--Duration of the '>='EXEC AddFilter @TraceID, 'SQLSecurityLoginName', 'srCXPTest',--Duration of the 'NOT LIKE'EXEC AddFilter @TraceID, 'NTUserName', 'srCXPTest',--Duration of the 'NOT LIKE'EXEC AddFilter @TraceID, 'ApplicationName', 'MS SQLEM',--Duration of the 'NOT LIKE'EXEC AddFilter @TraceID, 'TextData', 'p_getNextClarifyAPIRequest',--Duration of the 'NOT LIKE'--exec p_getNextClarifyAPIRequest--MS SQLEM--EXEC AddFilter --@TraceID, --'DatabaseID', --6,--Duration of the --'='EXEC StartTrace @TraceIDGO--CODE TO STOP A TRACECREATE PROC dbo.StopTrace( @TraceID int)ASBEGIN SET NOCOUNT ON DECLARE @ReturnValue int, @Stop int DECLARE @ProcedureName varchar(25), @Error varchar(100) DECLARE @TraceErrors table (Error int, [Description] varchar(100)) SET @ProcedureName = 'StopTrace' SET @Stop = 0 IF NOT EXISTS ( SELECT 1 FROM ::fn_trace_getinfo(@TraceID) ) BEGIN /*RAISERROR('Cannot find trace with ID %d. Source: %s', 16, 1, @TraceID, @ProcedureName) RETURN -1*/ RETURN END INSERT INTO @TraceErrors (Error, [Description]) VALUES (1, 'Unknown error') INSERT INTO @TraceErrors (Error, [Description]) VALUES (8, 'The specified Status is not valid') INSERT INTO @TraceErrors (Error, [Description]) VALUES (9, 'The specified Trace Handle is not valid') INSERT INTO @TraceErrors (Error, [Description]) VALUES (13, 'Out of memory') EXEC @ReturnValue = sp_trace_setstatus @traceid = @TraceID, @status = @Stop IF @ReturnValue <> 0 BEGIN SET @Error = (SELECT [Description] FROM @TraceErrors WHERE Error = @ReturnValue) SET @Error = COALESCE(@Error, 'Unknown error ' + CAST(@ReturnValue AS varchar(10))) RAISERROR('Failed to stop trace. Error: %s. Source: %s', 16, 1, @Error, @ProcedureName) RETURN -1 END RETURN 0ENDGOMike"oh, that monkey is going to pay" |
 |
|