Custom Search
body { background:#fff url("http://www.blogblog.com/harbor/rocks_left.jpg") no-repeat right bottom; background-attachment:fixed; margin:0; padding:0; font:x-small Georgia, Serif; color:#333333; font-size/* */:/**/small; font-size: /**/small; } /* Commented Backslash Hack hides rule from IE5-Mac \*/ body {background-attachment:scroll;} /* End IE5-Mac hack */ a:link { color:#336688; text-decoration:none; } a:visited { color:#764; text-decoration:none; } a:hover { color:#993333; text-decoration:underline; } a img { border-width:0; } /* Page Structure ----------------------------------------------- */ #wrap { background:url("http://www.blogblog.com/harbor/sky_left.jpg") repeat-x; min-width:740px; margin:0; padding:0; text-align:left; font: normal normal 100% Georgia,Serif; } #wrap2 { background:url("http://www.blogblog.com/harbor/lighthouse_left.jpg") no-repeat left 0px; } #wrap3 { background:url("http://www.blogblog.com/harbor/cloud_left.jpg") no-repeat right 75px; } #wrap4 { background:url("http://www.blogblog.com/harbor/center_cloud_left.jpg") no-repeat 50% 0px; padding:15px; width:100%; width/* */:/**/auto; width: /**/auto; } #outer-wrapper { max-width:890px; padding: 0 30px 50px; width:100%; width/* */:/**/auto; width: /**/auto; } html>body #outer-wrapper { border:3px double #fff; } #main-wrapper { width:64%; float:right; word-wrap: break-word; /* fix for long text breaking sidebar float in IE */ overflow: hidden; /* fix for long non-text content breaking IE sidebar float */ } #main { margin:0; padding:0; } #sidebar-wrapper { width:32%; float:left; word-wrap: break-word; /* fix for long text breaking sidebar float in IE */ overflow: hidden; /* fix for long non-text content breaking IE sidebar float */ } #sidebar { margin:0; padding-top: 170px; } /** Page structure tweaks for layout editor wireframe */ body#layout #outer-wrapper, body#layout #sidebar, body#layout #wrap4, body#layout #header { margin-top: 0; margin-bottom: 0; padding: 0; } body#layout #sidebar-wrapper { width: 180px; margin-left: 0; } body#layout #wrap4, body#layout #outer-wrapper { width: 650px; } /* Header ----------------------------------------------- */ #header { padding-top:15px; padding-right:0; padding-bottom:10px; padding-left:110px; position: relative; } .Header h1 { margin:0 0 .25em; color:#667788; font: normal normal 270% Georgia, Serif; } .Header h1 a { color:#667788; text-decoration:none; } .Header .description { margin:0; max-width:700px; line-height:1.8em; text-transform:uppercase; letter-spacing:.2em; color:#667788; font: normal normal 75% Georgia, Serif; } /* Headings ----------------------------------------------- */ h2 { margin:1.5em 0 .75em; line-height: 1.4em; font: normal normal 78% Georgia, Serif; text-transform:uppercase; letter-spacing:.2em; color:#993333; } /* Posts ----------------------------------------------- */ h2.date-header { margin:2em 0 .5em; color: #993333; font: normal normal 78% Georgia, Serif; } .post { margin:.5em 0 1.5em; } .post h3 { margin:.25em 0 0; padding:0 0 4px; font-size:140%; font-weight:normal; line-height:1.4em; } .post h3 a, .post h3 strong { background:url("http://www.blogblog.com/harbor/icon_lighthouse.gif") no-repeat left .15em; display:block; padding-left:20px; text-decoration:none; color:#336688; font-weight:normal; } .post h3 strong { background-image:url("http://www.blogblog.com/harbor/icon_lighthouse2.gif"); color:#000; } .post h3 a:hover { color:#993333; } .post-body { background:url("http://www.blogblog.com/harbor/divider.gif") no-repeat center top; padding-top:12px; margin:0 0 .75em; line-height:1.6em; } .post-body blockquote { line-height:1.3em; } .post-footer { color:#999; text-transform:uppercase; letter-spacing:.1em; font-size: 78%; line-height: 1.4em; } .comment-link { margin-left:.4em; } .post-footer .post-timestamp, .post-footer .post-author { color:#666; } .comment-link strong { font-size:130%; } .comment-link { margin-left:.4em; } .post img { padding:4px; border:1px solid #cde; } /* Comments ----------------------------------------------- */ #comments { background:url("http://www.blogblog.com/harbor/divider.gif") no-repeat center top; padding:15px 0 0; } #comments h4 { margin:1em 0; font-weight: bold; line-height: 1.6em; text-transform:uppercase; letter-spacing:.2em; color: #993333; font: bold 78% Georgia Serif; } #comments h4 strong { font-size:130%; } #comments-block { margin:1em 0 1.5em; line-height:1.4em; } #comments-block dt { margin:.5em 0; } #comments-block dd { margin:.25em 20px 0; } #comments-block dd.comment-timestamp { margin:-.25em 20px 1.5em; line-height: 1.4em; text-transform:uppercase; letter-spacing:.1em; } #comments-block dd p { margin:0 0 .75em; } .deleted-comment { font-style:italic; color:gray; } .feed-links { clear: both; line-height: 2.5em; } #blog-pager-newer-link { float: left; } #blog-pager-older-link { float: right; } #blog-pager { text-align: center; } .comment-footer { font: 78%/1.4em Georgia , Serif; } /* Sidebar Content ----------------------------------------------- */ .sidebar .widget, .main .widget { background:url("http://www.blogblog.com/harbor/divider.gif") no-repeat center bottom; margin:0 0 15px; padding:0 0 15px; } .main .Blog { background-image: none; } .sidebar ul { list-style:none; margin-left: 0; } .sidebar li { margin:0; padding-top:0; padding-right:0; padding-bottom:.25em; padding-left:15px; text-indent:-15px; line-height:1.5em; } .sidebar p { color:#666; line-height:1.5em; } /* Profile ----------------------------------------------- */ .profile-datablock { margin:.5em 0 .5em; } .profile-data { margin:0; font: normal normal 78% Georgia, Serif; font-weight: bold; line-height: 1.6em; text-transform:uppercase; letter-spacing:.1em; } .profile-img { float: left; margin-top: 0; margin-right: 5px; margin-bottom: 5px; margin-left: 0; padding: 4px; border: 1px solid #cde; } .profile-textblock { margin:.5em 0 .5em; } .profile-link { font:78%/1.4em Georgia,Serif; text-transform:uppercase; letter-spacing:.1em; } /* Footer ----------------------------------------------- */ #footer-wrapper { clear:both; padding-top:15px; padding-right:30px; padding-bottom:0; padding-left:50px; text-align: center; } #footer .widget { background:url("http://www.blogblog.com/harbor/divider.gif") no-repeat center top; margin:0; padding-top:15px; line-height: 1.6em; text-transform:uppercase; letter-spacing:.1em; } -->

Thursday, June 19, 2008

Scripting Trace's for SQL 2005

I wrote this with the assumption that you will be running traces around job steps, but you can adapt this for other situations.I used the default traces for both SQL and AS traces, but removed most of the Events from both the trace definitions in this write-up, just for brevity, you can generate the full definitions from Profiler by following the steps below. I have sections for SQL Server and Analysis Services.

SQL Trace

1. Create the Trace you want in SQL Profiler, on a SQL connection, setting a Trace Name (I used 'OLAP Error SQL Data'), Save To File with a file name and a file size large enough to hold the data in a single file (I used 32MB), and the Events / Data fields you want to trace. Run the Trace.

2. Stop the Trace - we don't actually want to collect anything at this point.

3. Export the trace definition - select "FileExportScript Trace DefintionFor SQL 2005...". Export the definition to a .SQL file of your choice.

4. Close Profiler, we're done with it at this point.In the SQL job with the step you want to trace, you're going to add a couple of steps before the step you want to trace...

5. Add the following code to a T-SQL job step, named 'Delete SQL Trace File', before the step you want to trace (this step is just to make sure there isn't a file from a previous run, just in case):
DECLARE @OS_Cmd VARCHAR(1024)SELECT @OS_Cmd = 'DEL /Q /F "C:\Trace Files\OLAP Error SQL Data.trc"'EXECUTE master.dbo.xp_cmdshell @OS_CmdNOTE - you will get an error code 12 from the sp_trace_create call in the next step if the output file already exists.


6. Add the code from the SQL file you just created to a T-SQL job step, named 'Start SQL Trace', immediately after the 'Delete SQL Trace File' step. Replace the literal 'InsertFileNameHere' with the destination you want for the trace file (don't add .trc to the end of the file name - it will automatically get added for you). It should look like this:

-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 32-- Please replace the text InsertFileNameHere, with an appropriate
--filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
--will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share
exec @rc = sp_trace_create @TraceID output, 0, N'C:\Trace Files\OLAP Error SQL Data', @maxfilesize, NULL
if (@rc != 0) goto error
-- Client side File and Table cannot be scripted
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 14, 1, @on
exec sp_trace_setevent @TraceID, 14, 9, @on
exec sp_trace_setevent @TraceID, 14, 6, @on
exec sp_trace_setevent @TraceID, 14, 10, @on
exec sp_trace_setevent @TraceID, 14, 14, @on
exec sp_trace_setevent @TraceID, 14, 11, @on
exec sp_trace_setevent @TraceID, 14, 12, @on
-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 1745445d-46a5-4050-9922-16caf3851690'
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1
-- display trace id for future references
select
TraceID=@TraceID
goto finish
error: select
ErrorCode=@rc
finish:
Now you're going to add a couple of steps after the step you want to trace...

7. You need to stop the trace. Add this code to a T-SQL step named 'Stop SQL Trace', note that the file name in the WHERE clause must match the file name you specified above in the 'Start SQL Trace' step, with the '.trc. added to the end:
DECLARE @TraceID INT
SELECT @TraceID = TraceID FROM :: fn_trace_getinfo(0)WHERE CAST([value] AS VARCHAR(256)) = 'C:\Trace Files\OLAP Error SQL Data.trc'
IF @TraceID IS NOT NULL

BEGIN
PRINT 'Closing Trace ID ' + CAST (@TraceID AS VARCHAR(5))
EXEC sp_trace_setstatus @TraceID, 0

EXEC sp_trace_setstatus @TraceID, 2
END

No comments: