Well,
I’m creating a job that checks the backup routines and send and e-mail whenever it stops (I hope that will become useful to others).
That’s the way I’m doing:
1) I created a query that returns the db_name and the backup_type that had not been backed up within a certain period:
select name as "Nome da Base" , 'L' as Tipo from sys.sysdatabases a
where name not in ('master','tempdb','model'))
except
(select b.database_name, b.type from msdb..backupset b
where b.backup_start_date >= DATEADD (hour,-2, GETDATE())
and type='L')
union all
(select name as "Nome da Base", 'D' as Tipo from sys.sysdatabases a
where name not in ('tempdb','model'))
except
(select b.database_name, b.type from msdb..backupset b
where b.backup_start_date >= DATEADD (day,-1, GETDATE())
and type='D'
2) Created a table to record the returning rows of the query above:
CREATE TABLE [dbo].[Alerta_Log_Bkp](
[Nome da Base] [nvarchar](50) NULL,
[Tipo] [nvarchar](8) NULL,
[Servidor] [nvarchar](10) NULL,
[Hora Verificação] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Alerta_Log_Bkp] ADD CONSTRAINT [DF_Alerta_Log_Bkp_Servidor]
DEFAULT (N'Server_NAME') FOR [Servidor]
GO
ALTER TABLE [dbo].[Alerta_Log_Bkp] ADD CONSTRAINT [DF_Alerta_Log_Bkp_Último Backup]
DEFAULT (getdate()) FOR [Hora Verificação]
GO
3) Created a job with an Insert Select:
INSERT INTO [dbo].[Alerta_Log_Bkp]
([Nome da Base],[Tipo])
(select name as "Nome da Base" , 'L' as Tipo from sys.sysdatabases a
where name not in ('master','tempdb','model','BA_CMA_OM'))
except
(select b.database_name, b.type from msdb..backupset b
where b.backup_start_date >= DATEADD (hour,-2, GETDATE())
and type='L')
union all
(select name as "Nome da Base", 'D' as Tipo from sys.sysdatabases a
where name not in ('tempdb','model'))
except
(select b.database_name, b.type from msdb..backupset b
where b.backup_start_date >= DATEADD (day,-1, GETDATE())
and type='D')
GO
–> OK, till this point everything is fine.
My problem lies on the creation of a trigger on the “Alerta_Log_Bkp” table that fires an e-mail when a line is inserted on it. If I test the insert with only 1 row, everything works fine. But, if the insert has more than 1 row, I get the following error:
“Subquery returned more than 1 value. This is not permitted when the subquery “
I know that’s happening because I’m setting a variable as a select from inserted. But I cannot figure another way out.
Here’s the trigger:
Create TRIGGER [dbo].[TR_Alert_mail_BKP] ON [dbo].[Alerta_Log_Bkp] AFTER INSERT AS
DECLARE @base varchar(50)
DECLARE @tipo varchar(50)
DECLARE @servidor varchar(50)
DECLARE @last_bkp DATETIME
SET @base = (SELECT "Nome da Base" FROM inserted)
SET @tipo = (SELECT tipo FROM inserted)
SET @servidor = (SELECT Servidor FROM inserted)
create table #temp (base varchar(50),
tipo varchar(50),
servidor varchar(50))
insert into #temp values (@base,@tipo,@servidor)
SET @last_bkp = (Select MAX(backup_start_date) from msdb..backupset
Where database_name = (select base from #temp)
and type = (select tipo from #temp))
--SET @last_bkp = (Select MAX(backup_start_date) from msdb..backupset
-- Where database_name = @base and type = @tipo)
IF @tipo in ('D','L')
BEGIN
DECLARE @msg varchar(8000)
SET @msg = 'The "' +@tipo + '" from database "' + @base + '", of the server "'
+ @servidor + '", stoped at ' + CAST(@last_bkp as varchar(50)) +''
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'my_mail@domain.com',
@body= @msg,
@subject = 'Problemas no Backup' ,
@profile_name = 'PROFILE'
END
Does anyone have a clue of another way to write that trigger, so I won’t get the subquery error?
Thanks,
Fabrício
Here is my suggestion. Drop the trigger. Add a column to the Alerta_Log_Bkp table:
Create the following stored procedure:
Now call the stored procedure from the job. No trigger, no muss, no fuss. And just one single e-mail even if there are 40 databases that violate the rule.