在ALTER_PROCEDURE DDL Trigger

时间:2018-01-25 23:03:23

标签: mysql sql sql-server tsql

我创建了一个SQL触发器,可以在我的数据库中的任何存储过程发生更改时发送电子邮件警报。我可以在电子邮件中包含SP的名称吗?如何调用特定的SP名称才能显示在电子邮件中?

这是我的代码:

ALTER TRIGGER [send_Email_to_User_ALTER]
ON DATABASE--msdb.dbo.[sp_send_dbmail]
AFTER ALTER_PROCEDURE
AS

--BEGIN 
--SET NOCOUNT ON;

EXEC msdb.dbo.[sp_send_dbmail]
@profile_name = 'Test Alert',
@recipients = 'email@outlook.com',
@body = 'A STORED PROCEDURE HAS BEEN ALTERED.'  , 
@subject = 'ALERT System SP Change Notification';  

1 个答案:

答案 0 :(得分:0)

试试这个:

ALTER TRIGGER [send_Email_to_User_ALTER] 
ON DATABASE--msdb.dbo.[sp_send_dbmail]
AFTER ALTER_PROCEDURE
AS


--BEGIN 
--SET NOCOUNT ON;
DECLARE @data XML
SET @data = EVENTDATA()      
DECLARE @objName Nvarchar(max)
SET @objName = (SELECT  @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'nvarchar(256)'))

DECLARE @body NVARCHAR(MAX)
SET @body = 'A STORED PROCEDURE HAS BEEN ALTERED.' + @objName 

EXEC msdb.dbo.[sp_send_dbmail]
@profile_name = 'Test Alert',
@recipients = 'email@outlook.com',
@body = @body , 
@subject = 'ALERT System SP Change Notification';