tsql知道上次在数据库上运行dbcc checkdb的时间

时间:2010-05-13 23:49:32

标签: sql-server dbcc

使用Tsql,我怎么知道DBCC checkdb最后在SQL服务器上运行的时间(2000,2005或2008)?

此致

7 个答案:

答案 0 :(得分:6)

2005年及以后你可以运行

DBCC DBINFO ('YourDatabaseName') WITH TABLERESULTS

寻找dbi_dbccLastKnownGood

答案 1 :(得分:3)

尝试此操作以检查SQL 2005以上的所有数据库

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

答案 2 :(得分:2)

来自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
  

你需要寻找   dbi_dbccLastKnownGood字段。那是   DBCC CHECKDB最后一次运行   没有发现任何腐败。

答案 3 :(得分:1)

从SQL Server 2016 SP2开始,您可以使用:

SELECT  DATABASEPROPERTYEX(DB_NAME(), 'LastGoodCheckDbTime')

DATABASEPROPERTYEX

  

LastGoodCheckDbTime

     

上次成功运行DBCC CHECKDB的日期和时间。

     

NULL =输入无效基本数据类型:datetime

答案 4 :(得分:0)

上述查询产生的结果不正确,因为#temp表未在游标中截断

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

答案 5 :(得分:0)

以防万一有人需要在任何2005+的机器上运行,就像一次性执行每个DB一样:

IF OBJECT_ID('tempdb..#DBCC_Out', 'U') IS NOT NULL DROP TABLE #DBCC_Out;
IF OBJECT_ID('tempdb..#Out', 'U') IS NOT NULL DROP TABLE #Out;

CREATE TABLE #DBCC_Out (ParentObject varchar(255), [Object] varchar(255), [Field] varchar(255), [Value] varchar(255))
CREATE TABLE #Out ([DB_Name] varchar(255), [Last_DBCCCheckDB] datetime)

EXECUTE sp_MSforeachdb 
'
 INSERT INTO #DBCC_Out EXECUTE (''DBCC DBINFO ( ''''?'''' ) WITH TABLERESULTS'')
 INSERT INTO #Out ([DB_Name], [Last_DBCCCheckDB])
 (SELECT ''?'', [Value] FROM #DBCC_Out WHERE [Field] = ''dbi_dbccLastKnownGood'');
 TRUNCATE TABLE #DBCC_Out;
'
 DROP TABLE #DBCC_Out;
 SELECT * FROM #Out;

答案 6 :(得分:0)

如果使用Ola Hallengren的维护脚本,则该信息将保存在命令日志表中。以下是如何获得DBCC在过去一周中成功运行的信息

使用母版

选择* 从CommandLog 其中CommandType ='DBCC_CHECKDB' 和(CONVERT(datetime,StartTime,102)> = GETDATE()-7) 按开始时间排序 结束时间