Skip to main content

Script to find out when last back is taken and when a last successful DBCC CHECKDB is executed…

Use the below code to identify the last backup execution time and last time when
DBCC CHECKDB was successfully executed (which means when last DBCC CHECKDB executed last time and reported no corruption) on all databases of the SQL Instance. This code will work (for CHECKDB part) only from SQL Server 2005 onwards.

Typically I use this handy code for server’s health check or for some ad-hoc activity where these informations are needed immediately. You may use this code in many cases as per requirement of your environment. If you are interested on DBCC part of this code, please refer to for details.

USE master
ParentObject nvarchar(4000) null,
Object nvarchar(4000) null,
Field nvarchar(4000) null,
VALUE nvarchar(4000) null
[Database Name] nvarchar(4000) null,
[Last Known Good DBCC] nvarchar(4000) null
      DECLARE @cmd varchar(4000)
      DECLARE @DB_NAME nvarchar(500)
      DECLARE @DB_ID int
      SELECT name, [dbid] FROM sysdatabases
      ORDER BY dbid
      OPEN LastDBCC_cursor
      FETCH NEXT FROM LastDBCC_cursor into @DB_NAME, @DB_ID
      SET @cmd = 'dbcc page('+ convert(varchar,@DB_ID)+',1,9,3) with tableresults'
      insert into #tbl_DBCC execute (@cmd)
      insert into #tbl_LastDBCC
      select distinct @DB_NAME, VALUE
      from #tbl_DBCC
      where Field = 'dbi_dbccLastKnownGood'
      if @@ROWCOUNT = 0
           insert into [#tbl_LastDBCC] select @DB_NAME, 'Not implemented'

      FETCH NEXT FROM LastDBCC_cursor into @DB_NAME, @DB_ID
      delete #tbl_DBCC
      CLOSE LastDBCC_cursor
      DEALLOCATE LastDBCC_cursor
      select T1.[Database Name],
       WHEN (max(T1.[Last Known Good DBCC]) = '1900-01-01 00:00:00.000') then 'DBCC Not executed yet!'
                  ELSE max(T1.[Last Known Good DBCC])
            END as [Last Known Good DBCC],
            COALESCE(convert(varchar(50),MAX(T2.backup_finish_date),21),'No Backup Taken Yet!') AS [Last BackUp Taken]
      from #tbl_LastDBCC T1 LEFT OUTER JOIN msdb.dbo.backupset T2
      ON T2.database_name = T1.[Database Name]
      GROUP BY T1.[Database Name]
      ORDER BY T1.[Database Name]
      DROP TABLE #tbl_LastDBCC
      DROP TABLE #tbl_DBCC
      DBCC traceoff(3604)

Reference taken: PFE SQL Server Blog to develop the above code.


Popular posts from this blog

How to kill a negative SPID (like SPID -2) in SQL Server?

Rarely this scenario will arise when most likely you see this negative SPID (most likely SPID -2) is blocking other transaction causing issues. If you try to kill it using normal KILL command, it will fail reporting below error: Msg 6101, Level 16, State 1, Line 1 Process ID <SPID Number> is not a valid process ID. Choose a number between 1 and 2048 This is because of an orphaned distributed transaction ID.  Please follow below steps to kill it: Step 1: -- Find the UOW Number select req_transactionUOW from master..syslockinfo where req_spid = <SPID Number> --  <SPID Number>  is -2 most likely. Step 2: -- Copy the UOW number from Step one KILL ‘<UOW Number>’ This will kill the negative SPID resolving the issue.  However please note following points: 1. For SPID -2, you may find multiple UOW numbers. Please start killing them one by one. Typically killing first UOW will resolve the issues. (ie. will kill all UOW and release

DMV/TSQL to find out basic hardware information of the SQL Server including when SQL Server started.

Please use below code: However, please be advised that it can not tell correct information around virtualization.  For example, it will show Hypervisor even if SQL runs on a physical OS where Hyper-V is on. So use this query only when you do not have sufficient access on underlying Windows Operating system to get these information directly. -- Basic hardware information for SQL Server (sys.dm_os_sys_info) /* This query is courtesy of All credits goes to original author. */ SELECT cpu_count AS [Logical CPU Count] , scheduler_count , hyperthread_ratio AS [Hyperthread Ratio] , cpu_count / hyperthread_ratio AS [Physical CPU Count] , physical_memory_kb / 1024 AS [Physical Memory (MB)] , committed_kb / 1024 AS [Committed Memory (MB)] , committed_target_kb / 1024 AS [Committed Target Memory (MB)] , max_workers_count AS [Max Workers Count] , affinity_type_desc AS [Affinity Type] , sqlserver_start_time AS [

‘Trace Skipped Records’ – What is this and how to resolve it while using SQL Server Profiles?

In some very rare case, you may experience a very weired message in profiler’s output as ‘Trace Skipped Records’ while you trace something on SQL Server. Screenshot of similer situation is as below: This is not an error but it comes by design of SQL Server (I believe so). When you are using SQL profiler and return data is too big to fit in the GUI (for me, it is an enormous xml), SQL Server simply prints this message and proceed to next step. Mostlikely this is to save server’s memory and performance. Although not suggested and guranteed, you can try to run a server side trace and dump data in a file which should capture all the data. However, it is strongly not recommended to run a trace on your production server from server side. Microsoft will probally document this limitation in future. More details may be found at