SQL Server essence

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


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

posted on 2008-03-27 12:25  天蝎  阅读(176)  评论(0编辑  收藏  举报