Saturday, January 7, 2017

CheckDB

-- Last good checkdb on all databases.

create table #tmp1
(
ParentObject nvarchar(100),
Object nvarchar(100),
Field nvarchar(100),
Value nvarchar(100)
)

create table #tmpF
(
name nvarchar(100),
Value datetime
)

declare @a int
declare @b int
declare @c varchar(50)
declare @datetime datetime
select name,Row_number()over(order by database_id) id into #tmp from sys.databases where state<>6
set @a = 1
select @b = MAX(id) from #tmp
while(@a<= @b)
begin
select @c = name from #tmp where id =@a
insert into #tmp1(ParentObject,Object,Field,Value) execute ('dbcc dbinfo(' + @c +') with tableresults')
set @datetime = (select distinct Value from #tmp1 where Field like '%dbi_dbccLastKnownGood%')
insert into #tmpF values (@c,@datetime)
truncate table #tmp1
set @datetime = null
set @c = null
set @a = @a + 1
end
select @@SERVERNAME [Server Name],name as [Database Name], [value] as [Last Good CheckDb] from #tmpF
drop table #tmp
drop table #tmp1
drop table #tmpF

No comments:

Post a Comment

New Features in SQL server 2016

This summary is not available. Please click here to view the post.