I am sure I posted it here at least once, but here's another version:
declare @path nvarchar(1024)
select @path = [path] from sys.traces where is_default = 'true'
set @pattern = reverse(substring(reverse(@path), 1, charindex(N'_', reverse(@path))))
if cast(replace(replace(@pattern, N'_', N''), N'.trc', N'') as int) <= 4
set @replacement = N'_1.trc'
set @replacement = N'_' + cast(cast(replace(replace(@pattern, N'_', N''), N'.trc', N'') as int) - 4 as nvarchar(10)) + N'.trc'
set @firstfile = replace(@path, @pattern, @replacement)
select * from ::fn_trace_gettable(@firstfile, 0)
Last edited by rdjabarov; 01-19-11 at 17:10.
Reason: changed from < 4 to <= 4
"The data in a record depends on the Key to the record, the Whole Key, and
nothing but the Key, so help me Codd."