Using Tsql, how can i know when DBCC checkdb was last run on SQL server (2000, 2005 or 2008)?
Regards
Approximately half-way down, at line 51, you'll see a line where the third column (called Field in the output) shows dbi_dbccLastKnownGood. That's what you're looking for. The forth column for that row, called Value, shows the date that DBCC CHECKDB last completed successfully.
If you see DBCC ALLOC CHECK, wait a bit as the DBCC TABLE CHECK occurs after the ALLOC CHECK. If you ran CheckDB in SSMS you don't need to run the first query, you can just look at the name of the tab in SSMS to find the sessionID.
FULL CHECKDB with 64 cores: takes 7 minutes, checks everything.
The shorter the period of time you keep backups, the more often you need to run DBCC CHECKDB. If you keep data for two weeks, weekly is a good starting point. If you take weekly fulls, you should consider running your DBCC checks before those happen. A corrupt backup doesn't help you worth a lick.
on 2005 and up you can run
DBCC DBINFO ('YourDatabaseName') WITH TABLERESULTS
look for dbi_dbccLastKnownGood
Try this to check all DB's on SQL 2005 onwards
SET NOCOUNT ON;
DBCC TRACEON (3604);
CREATE TABLE #temp (
Id INT IDENTITY(1,1),
ParentObject VARCHAR(255),
[Object] VARCHAR(255),
Field VARCHAR(255),
[Value] VARCHAR(255)
)
CREATE TABLE #Results (
DBName VARCHAR(255),
LastGoodDBCC VARCHAR(255)
)
DECLARE @Name VARCHAR(255);
DECLARE looping_cursor CURSOR
FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE CONVERT(varchar(500),databasepropertyex(name, 'Status'),0) = 'ONLINE'
OPEN looping_cursor
FETCH NEXT FROM looping_cursor INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #temp
EXECUTE('DBCC PAGE (['+@Name+'], 1, 9, 3)WITH TABLERESULTS');
INSERT INTO #Results
SELECT @Name,MAX(VALUE) FROM #temp
WHERE Field = 'dbi_dbccLastKnownGood';
FETCH NEXT FROM looping_cursor INTO @Name
END
CLOSE looping_cursor;
DEALLOCATE looping_cursor;
SELECT DBName
,ISNULL(LastGoodDBCC,'1900-01-01 00:00:00.000') AS 'LastGoodDBCC'
FROM #Results
DROP TABLE #temp
DROP TABLE #Results
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With