tsql 了解上次在数据库上运行 dbcc checkdb 的时间

发布于 2024-09-01 11:27:32 字数 87 浏览 6 评论 0 原文

使用 Tsql,我如何知道 DBCC checkdb 最后一次在 SQL Server 上运行的时间(2000、2005 或 2008)?

问候

Using Tsql, how can i know when DBCC checkdb was last run on SQL server (2000, 2005 or 2008)?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独孤求败 2024-09-08 11:27:32

在 2005 及更高版本上,您可以运行

DBCC DBINFO ('YourDatabaseName') WITH TABLERESULTS

查找 dbi_dbccLastKnownGood

on 2005 and up you can run

DBCC DBINFO ('YourDatabaseName') WITH TABLERESULTS

look for dbi_dbccLastKnownGood

素年丶 2024-09-08 11:27:32

尝试检查 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

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
段念尘 2024-09-08 11:27:32

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

SELECT  DATABASEPROPERTYEX(DB_NAME(), 'LastGoodCheckDbTime')

DATABASEPROPERTYEX

最后良好检查数据库时间

上次在指定数据库上成功运行 DBCC CHECKDB 的日期和时间。

NULL = 输入无效基本数据类型:日期时间

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

作死小能手 2024-09-08 11:27:32

来自 http://www.sqlskills.com/BLOGS/PAUL/post/CHECKDB-From-Every-Angle-When-did-DBCC-CHECKDB-last-run-successively.aspx

DBCC TRACEON (3604); 
GO

-- page 9 is the boot page
DBCC PAGE (dbname, 1, 9, 3);
GO

您需要寻找
dbi_dbccLastKnownGood 字段。那是
上次运行 DBCC CHECKDB 的时间
没有发现任何损坏。

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.

幻梦 2024-09-08 11:27:32

上面的查询产生不正确的结果,因为 #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

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
泅渡 2024-09-08 11:27:32

如果您使用 Ola Hallengren 的维护脚本,则此信息将保存在 CommandLog 表中。以下是如何获取上周的所有 DBCC_CHECKDB 执行:

USE [master]
GO

SELECT *
FROM [CommandLog]
WHERE [CommandType] ='DBCC_CHECKDB'
AND (CONVERT([datetime], [StartTime], 102) >= GETDATE() - 7) 
ORDER BY [StartTime], [EndTime]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文