Friday, October 09, 2009

A little TSQL fun

So I found a script that did a dbcc checkdb in a 'data driven' manor... it was kinda slick but it assumes you can check all of your database in one swoop. Well, I can't. SOOOOoooooooo I decided to do a little date trickery.

The trickery is simple, I have x number of databases, 7 days of the week. I split them evenly along each of the 7 days. so I do y databases a day based upon the day of the week.

The code is pretty easy to follow, I have only run it on SQL 2008 and have yet to put it into production.. so here goes the code


It was formatted, but I know blogger will kill the formatting:


declare @StartID int = (select ((COUNT(*))/7) * DATEPART(weekday,getdate()) from master..sysdatabases),
@EndID int = (select (select (COUNT(*)/7) * DATEPART(weekday,getdate()) from master..sysdatabases)+ (select (COUNT(*))/7 from master..sysdatabases)-1)

declare database_cursor CURSOR for select name from master..sysdatabases where dbid between @StartID and @EndID
declare @database_name sysname

open database_cursor
fetch next from database_cursor into @database_name
while @@FETCH_STATUS=0
begin
print @database_name
dbcc checkdb(@database_name) with no_infomsgs
fetch next from database_cursor into @database_name
end

close database_cursor
deallocate database_cursor

Labels: , , ,

1 Comments:

Blogger TedS said...

This code does indeed work well... but it makes a dangerous assumption that I can't take. That is that doing the databases in order of their creation is ok. Problem is, well, I have several larger databases right next to each other in that list..... yeah... so I am going to re-work the sorting.

11:21 AM  

Post a Comment

<< Home