|
|
|
|||||||
|
ICT-Hotlist TopicCheck your SQL server databases for errors.Using the undocumented1 sp_MSforeachdb stored procedure it is very easy to run database checks on all of your SQL databases. Microsoft recommends that you periodically run DBCC CHECKDB with no options. How frequently you should perform these runs, depends on individual businesses and their production environments.Just open a command window or create a (scheduled) batch file to run the command.
SQLCMD -q "EXECUTE master.sys.sp_MSforeachdb 'USE [?]; DBCC CHECKDB'" | FindStr
"CHECKDB Found"
What it does:It uses the standard Microsoft SQL-server command line interface SQLCMD to start a T-SQL query on the local database server with the credentials of the currently logged on user. (see other options of SQLCMD when you need to specify user information and/or server name)The command then uses the undocumented1 stored procedure sp_MSforeachdb to loop through all the databases. (The current or specified user must have the appropriate rights to access the database) Next the DBCC CHECKDB command is started using the name of the current database. Finally the FindStr command accepts all the SQL-server messages and filters out all the lines that contain the error status message. Example output:
CHECKDB found 0 allocation errors and 0 consistency errors in database 'master'.
In the rare occasion that an error is reported, run the DBCC CHECKDB command on the specific database to see all the details.
CHECKDB found 0 allocation errors and 0 consistency errors in database 'mssqlsystemresource'. DBCC CHECKDB will not check SQL Server catalog or Service Broker consistency because WITH TABLOCK was specified. CHECKDB found 0 allocation errors and 0 consistency errors in database 'tempdb'. CHECKDB found 0 allocation errors and 0 consistency errors in database 'model'. CHECKDB found 0 allocation errors and 0 consistency errors in database 'msdb'. CHECKDB found 0 allocation errors and 0 consistency errors in database 'ReBaUp'. SQLCMD errorIf you get error "HResult 0x2, Level 16, State 1 Named Pipes Provider: Could not open a connection to SQL Server [2]." running the SQLCMD on your system, try these tips: https://stackoverflow.com/questions/2357109/could-not-open-a-connection-to-sql-server1Of course undocumented implies unsupported and that there is no guarantee about whether the procedure will exist in future versions of SQL-Server. So, use at your own risk and discretion. Available in:
You may vote your opinion about this article:
Scripts and programming examples disclaimerUnless stated otherwise, the script sources and programming examples provided are copyrighted freeware. You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation. However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data. Make sure you have verified full backups and the associated restore software available before running any script or programming example. Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation. |