Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BrentOzar/0e9692da8ceffeb2b4bdd064e36d3174 to your computer and use it in GitHub Desktop.
Save BrentOzar/0e9692da8ceffeb2b4bdd064e36d3174 to your computer and use it in GitHub Desktop.
Looping through SQL Server trace flags to find the max one
DECLARE @CurrentTraceFlag INT = 1
/* Hard coding in a max number in case they get wise to this trick and start failing silently */
WHILE @CurrentTraceFlag < 20000
BEGIN
BEGIN TRY
DBCC TRACEON(@CurrentTraceFlag, -1);
DBCC TRACEOFF(@CurrentTraceFlag, -1);
END TRY
BEGIN CATCH
SELECT @CurrentTraceFlag, ERROR_NUMBER(), ERROR_MESSAGE();
BREAK
END CATCH
SET @CurrentTraceFlag = @CurrentTraceFlag + 1
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment