tsql to know when dbcc checkdb was run on a database last time
Using Tsql, how can i know when DBCC checkdb wa开发者_如何学Pythons last run on SQL server (2000, 2005 or 2008)?
Regards
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
Starting with SQL Server 2016 SP2 you could use:
SELECT DATABASEPROPERTYEX(DB_NAME(), 'LastGoodCheckDbTime')
DATABASEPROPERTYEX:
LastGoodCheckDbTime
The date and time of the last successful DBCC CHECKDB to run on the specified database.
NULL = Input not valid Base data type: datetime
From http://www.sqlskills.com/BLOGS/PAUL/post/CHECKDB-From-Every-Angle-When-did-DBCC-CHECKDB-last-run-successfully.aspx:
DBCC TRACEON (3604);
GO
-- page 9 is the boot page
DBCC PAGE (dbname, 1, 9, 3);
GO
You need to look for the dbi_dbccLastKnownGood field. That was the last time that DBCC CHECKDB ran without finding any corruptions.
The query above produces incorrect results because the #temp table is not truncated within the cursor
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';
truncate table #temp
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
If you use Ola Hallengren’s maintenance scripts then this information would be saved in the CommandLog
table. The below is how to get all DBCC_CHECKDB executions from the last week:
USE [master]
GO
SELECT *
FROM [CommandLog]
WHERE [CommandType] ='DBCC_CHECKDB'
AND (CONVERT([datetime], [StartTime], 102) >= GETDATE() - 7)
ORDER BY [StartTime], [EndTime]
精彩评论