Article ID: 115519 - Last Review: November 23, 2005 - Revision: 4.3 How to use the Findstr tool to scan SQL error log files or DBCC output files for analyzing errorsThis article was previously published under Q115519 SUMMARY
It is necessary to frequently examine DBCC output and the SQL Server error logs to
check for problems. This can be an error-prone and laborious process,
especially if you must do this on several servers, each of which may have
multiple databases. Windows NT ships with a powerful pattern-searching utility called
Findstr.exe that can be used to largely automate this task. You can also use this utility to conditionally execute a batch file based on whether certain error strings are found. This article describes how to use this Findstr.
MORE INFORMATION
Findstr is a high-performance pattern-searching utility that uses Win32
asynchronous and file-mapped I/O for best throughput. It allows the use of
regular expressions, recursive searching down a directory tree, and the
ability to exclude patterns, thus achieving a NOT function. See your Windows
NT documentation for additional information. There are two basic approaches to searching SQL Server error logs or DBCC output for errors using Findstr.exe:
For example, say you want to find all occurrences of the strings "table corrupt" (which accompanies many serious data errors) and "msg 605" (which is a specific type of error). Place these strings into a file so the content simply looks like this: The challenge with this approach is knowing in advance what error strings to search for. Unfortunately, there is no comprehensive list of SQL Server error strings that encompasses all possible DBCC, server, and kernel errors. For versions prior to 7.0, merely searching for these strings may provide fairly good coverage as a starting point: "table corrupt", "level 16"-"level 21", and "Severity: 16"-"Severity: 21." Using a little experimentation, it is usually possible to further refine this and get fairly reliable checking (certainly more reliable than doing so by eye). The other approach involves using the "reverse match" capability of Findstr to exclude a list of search strings. This approach is more easily applied to DBCC output than to error log output because there are fewer distinct strings that require exclusion. In some ways this approach, if applicable, is safer because you are only explicitly stating what not to match, and any new or unanticipated errors will be shown by default. Because of the currently limited experience with error message frequency for version 7.0, the "reverse match" approach is probably the best method for this version. For example, say you want to exclude the strings "The total number", "Table has", and "Checking" in a series of DBCC output files. This will exclude many non-essential DBCC status messages and will not result in loss of any errors. For versions before 7.0, place these strings into a file that looks like this: For 7.0, do similarly but use these strings: For example, say you are searching your error logs for the strings "Severity: 16" through "Severity: 21", which should match most serious errors. But you notice in your search output several communication- related errors such as 1608 and 17832, which are normally not indicative of a serious problem. You can keep your original search conditions and exclude only these specific unwanted errors from the search output by piping one Findstr command into another: It is possible to write a batch file that conditionally takes action based on whether Findstr detects a certain error string. This technique uses the Windows NT conditional processing commands which are "&&" and "||". The following command executes the batch file batch1.bat if any of the SQL Server error strings in the file search.txt are found: The techniques described above can be combined to perform a variety of tasks. For example, the following batch file will continuously run a Findstr command that conditionally executes batch1.bat if any of the SQL error strings in the file search.txt are found, excluding the strings in exclude.txt. Under some conditions, Findstr may encounter internal limits and be unable to complete a search. When this happens, you will see this message in the output:
FINDSTR: Write error.
A related topic is use of the Windows NT command-line tools. Starting with Windows NT 4.0, there are enhanced command extensions available. Do cmd /x /? to see the list. The enhanced FOR command is an example. Do for /? to see the syntax. Here is an example that shows how to use FOR to run isql 5 times in a row, saving each output to a differently named file: APPLIES TO
| Article Translations
|
Back to the top
