好,子查詢返回的SQL Server超過1個值2005觸發[2]
我創建檢查備份程序,每當它停止發送和郵件工作(我希望這將成爲有用給其他人)。
這就是我正在做的方式:
1)我創建了一個返回DB_NAME和尚未在一定期限內備份backup_type查詢:
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)創建一張表來記錄上面的查詢返回行:創建工作與插入選擇
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):
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
- >好的,直到現在一切都很好。
我的問題在於在「Alerta_Log_Bkp」表格上創建觸發器,該表格在插入線條時觸發電子郵件。如果我只用1行測試插入,一切工作正常。但是,如果插入的行數超過1行,我會收到以下錯誤: 「子查詢返回的值超過1個,當子查詢」
我不知道這是因爲我將變量設置爲從插入選擇。但我無法找到另一條出路。
這裏的觸發:
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 "' [email protected] + '" from database "' + @base + '", of the server "'
+ @servidor + '", stoped at ' + CAST(@last_bkp as varchar(50)) +''
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'[email protected]',
@body= @msg,
@subject = 'Problemas no Backup' ,
@profile_name = 'PROFILE'
END
沒有人有另一種方式來寫觸發線索,所以我不會得到子查詢的錯誤?
感謝, 法布里西奧
根據@HLGEM的建議,請參閱以下文章,瞭解爲什麼要避免從觸發器發送電子郵件 - 即使sp_send_dbmail類似地是異步的:http://sqlblog.com/blogs/aaron%5Fbertrand/ archive/2009/10/12/bad-habits-to-kick-abusing-triggers.aspx –