I want to execute some T-sql code, when a database starts up.
the objective is to resume a session of mirroring in case the session is "suspended" when the database comes "back to life".
So I've done this code:
begin try
Declare @state_desc nvarchar(60)
SELECT @state_desc = mirroring_state_desc FROM SYS.database_mirroring WHERE database_id = DB_ID('MyDataBase')
if @state_desc = 'SUSPENDED'
begin
ALTER DATABASE [MyDataBase] SET PARTNER RESUME
end
end try
begin catch
end catch
but how can I make it when the sql server starts up?
I used this with no success:
create procedure dbm_startup_resume
as
begin
begin try
Declare @state_desc nvarchar(60)
WAITFOR DELAY '00:00:10'
SELECT @state_desc = mirroring_state_desc FROM SYS.database_mirroring WHERE database_id = DB_ID('MinhaBaseDeDados')
if @state_desc = 'SUSPENDED'
begin
ALTER DATABASE [MinhaBaseDeDados] SET PARTNER RESUME
end
end try
begin catch
end catch
exec sp_procoption @ProcName = dbm_startup_resume, @OptionName = startup, @OptionValue = 'on'
end