Please wait,
Processing your request...

    0%
  Business logo VanSoest.it
  ... | Selecteer de Nederlandse taal |
Sharing is caring
| Print this page. | Linkedin page of Johan van Soest

React: Postcard image. Click this to mail to Johan




WebHalla
 Content
  Management
   System

ICT-Hotlist Topic

Check 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'.
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'.
In the rare occasion that an error is reported, run the DBCC CHECKDB command on the specific database to see all the details.

SQLCMD error

If 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-server
1Of 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:
  • SQL-Server 7.0 (Unsupported)
  • SQL-Server 2000 (Unsupported)
  • SQL-Server 2005
  • SQL-Server 2008
  • SQL-Server 2008R2
  • SQL-Server 2012
  • SQL-Server 2014
  • SQL-Server 2016
  • SQL-Server 2017
  • SQL-Server 2019
  • SQL-Server 2022
You may vote your opinion about this article:


Scripts and programming examples disclaimer

Unless 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.
Generated by WebHalla™ Version 0.1.e.7 : Tuesday 8-10-2024 © Copyright 1995-2024 ing. Johan P.G. van Soest CIPM Certified Privacy Information Manager
Response Form    Cookie- and Privacy statement    Responsible Disclosure procedure
Weather in Waalre by OpenWeatherMap logo light rain
Temperature 14.96 °C light rain
Wind chill 14.89 °C light rain
Humidity 91 % light rain
Air pressure 998 hPa light rain
Wind speed 5.14 m/s light rain
Wind direction South South light rain
Sun Rise 7:50 Sun Rise
Sun Set 19:00 Sun Set
Updated:2024-10-08 08:43:21 light rain
| Current user: Guest | Login |