Showing posts with label DBCC. Show all posts
Showing posts with label DBCC. Show all posts

Friday, December 5, 2014

Use DBCC HELP() To get the Syntax information of documented and undocumented DBCC Commands..

We are very familiar with the DBCC commands. But sometimes we forget their syntax information. Microsoft provides DBCC Help command in SQL Server which very few of us know . So, the next time when you are using DBCC command and you are confused with the syntax information just use DBCC HELP and it will give you all Syntax information.

Syntax for DBCC Help command is as follows:

DBCC HELP ('dbcc_statement' | @dbcc_statement_var | '?') [ WITH NO_INFOMSGS ]

dbcc_statement @dbcc_statement_var: Is the name of the DBCC command for which to receive syntax information. Provide only the part of the DBCC command that follows DBCC, for example, CHECKDB instead of DBCC CHECKDB.
? : Returns all DBCC commands for which Help is available.
WITH NO_INFOMSGS : Suppresses all informational messages that have severity levels from 0 through 10.
Suppose we  want to know the syntax of DBCC CHECKDB. Then execute the below command:
DBCC HELP (CHECKDB);
GO
It will give you result as below:
dbcc CHECKDB
(
{ 'database_name' | database_id | 0 }
[ , NOINDEX
| { REPAIR_ALLOW_DATA_LOSS
| REPAIR_FAST
| REPAIR_REBUILD
} ]
)
[ WITH
{
[ ALL_ERRORMSGS ]
[ , [ NO_INFOMSGS ] ]
[ , [ TABLOCK ] ]
[ , [ ESTIMATEONLY ] ]
[ , [ PHYSICAL_ONLY ] ]
[ , [ DATA_PURITY ] ]
[ , [ EXTENDED_LOGICAL_CHECKS ] ]
}
]

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

This way we  can see the syntax information.
If you will use '?' with DBCC Help it will returns all DBCC commands as below for which the help is available.
DBCC HELP('?')
GO

OUTPUT: checkalloc, checkcatalog, checkconstraints, checkdb, checkfilegroup, checkident,
checktable etc.....


But from the output we can see there are many DBCC commands which are not displayed in this result set. Like Page,addinstance, auditevent, autopilot etc.

Lets execute the DBCC help for the syntax of DBCC Page.

DBCC HELP(Page)
GO

Output: Msg 8987, Level 16, State 1, Line 1 No help available for DBCC statement 'Page'.


So what do you think SQL don't have any DBCC Page command. No its not like that. Its giving error because DBCC Page and other commands which were missing from previous results are the undocumented commands of microsoft Sql server.

To get the help for these undocumented commands you need to turn on the trace flag 2588. As DBCC Traceon(2588). It will turn the flag ON only for that session. If you want to turn it ON globally then you can execute this as DBCC Traceon(2588,-1).

So lets turn on this trace flag and then we'll execute DBCC Help(Page) to see the result.

DBCC Traceon(2588)
Go
DBCC Help(Page)
Go

Output:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
dbcc Page ( {'dbname' | dbid}, filenum, pagenum [, printopt={0|1|2|3} ])

DBCC execution completed. If DBCC printed error messages, contact your system administrator.



Yeahhhhh!!!! Now its giving syntax information for undocumented commands.

Now if you will execute the DBCC Help('?'). It will displays all the DBCC commands documented as well as undocumented.

DBCC Traceon(2588)
Go
DBCC Help('?')
Go


You can see the results in below screen shot.



Thanks For Reading this Blog!!!!!

References: Microsoft BOL

Sunday, October 26, 2014

How to know “the last time a DBCC CHECKDB was run without errors” in a Database

We all know the importance of DBCC CheckDB command in sql server. If data corruption occurred in a database and you want to know When was the last time DBCC CHECKDB reported no errors for the corrupted database.

If you have saved all CHECKDB results or the ERRORLOG files you can find it from there. 

But in case you have deleted these files the from where you will get to know the information about same???

Not to worry about this question as Along with CHECKDB reports and error log, SQL server saves information about the last time a DBCC CHECKDB was run without errors in the database also.

Let see how can we get that information:

I have a sample database TestVK for this post. Now to see the last known good information of TestVK database we will use dbcc command as below.

DBCC TRACEON(3604,-1)
DBCC DBINFO(TestVK)
DBCC TRACEOFF(3604,-1)




Go to Result and search for "LastKnownGood". You will see the output as below:

dbi_dbccLastKnownGood = 2014-10-27 10:20:33.833

So this is the last time when a DBCC CHECKDB was run without errors.

Thanks For Reading This Post!!