Please wait,
Processing your request...

    0%
  Business logo VanSoest.it  
  ... | Selecteer de Nederlandse taal | Print this page. | Linkedin page of Johan van Soest | User: Guest | Login

Postcard image. Click this to mail to Johan

WebHalla
 Content
  Management
   System


ICT-Hotlist Topic

Back to the ICT-Hotlist...
Don't miss latest updates Follow @WebHalla

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 'mssqlsystemr
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: http://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

You may vote your opinion about this article:


Topic created  :  01-05-2017
Topic last edited  :  11-05-2017

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.3 : Tuesday 18-6-2019 © Copyright 1995-2019 Johan van Soest
Response Form    Cookie- and Privacy statement
Weather in Waalre by OpenWeatherMap logo overcast clouds
Temperature 25.28 °C overcast clouds
Humidity 53 % overcast clouds
Air pressure 1015 hPa overcast clouds
Wind speed 1 m/s overcast clouds
Updated:2019-06-18 11:36:05 overcast clouds

Weather Cache is 2 minute(s) old.