if exists(select * from sys.objects where name like 'sp_waitfordbonline' and type ='P')
drop procedure sp_waitfordbonline
go
create procedure sp_waitfordbonline @dbname varchar(30), @timeout int
as
begin
-- waitfor db to come online
declare @status int
declare @loop int
declare @time int
select @time=0
select @loop=1
if (@timeout<=0)
print 'The minimum wait time for db to come online is set to 10 seconds'
while(@loop!=0)
begin
select @status=databaseproperty(@dbname,'IsInRecovery')
if(@status=0)
--Yukon and latest version
--select @desc=state_desc from sys.databases where name =@dbname
--if(@desc='ONLINE')
begin
select @loop=0
dbcc checkdb(@dbname) with no_infomsgs
end
else
begin
select @time = @time+1
waitfor delay '00:00:10'
end
if (@time>@timeout*6)
begin
select @loop=0
print 'Database:' + @dbname + ' did not come online in ' + convert(varchar(10),@time*10) + ' seconds'
end
end
end
go