我在DELTEK.MFG_BOM上创建了一个触发器,用于查找DELETK.MFG_BOM.MODIFIED_BY不在的表的UPDATE或INSERT(“C10863”,“M100133”,“M100196”,“M100273”,“ M100212“,”DELTEK“)
如果上述ID之一以外的某人更新或插入表中,它将发送电子邮件。
当用户测试时,他们会出现堕胎错误。这是什么原因。
这是我的触发器代码。
USE PRODCP7
GO
Create TRIGGER DELTEK.MFG_BOM_MODIFIED_BY on DELTEK.MFG_BOM
AFTER INSERT , UPDATE
AS
DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
Begin
if exists (Select 1 From inserted Where MODIFIED_BY NOT IN ('C10863','M100133','M100196','M100273','M100212', 'DELTEK') )
Begin
SELECT Inserted.ASY_PART_ID, Inserted.ASY_PART_RVSN_ID, DELTEK.ITEM.ITEM_DESC,
Inserted.MODIFIED_BY, DELTEK.MI_CP_USER_NAME_LOOKUP.NAME, Inserted.TIME_STAMP into #Tmp
FROM (Inserted
LEFT JOIN DELTEK.MI_CP_USER_NAME_LOOKUP ON Inserted.MODIFIED_BY = DELTEK.MI_CP_USER_NAME_LOOKUP.USER_ID)
LEFT JOIN DELTEK.ITEM ON Inserted.ASY_PART_KEY = DELTEK.ITEM.ITEM_KEY
GROUP BY Inserted.ASY_PART_ID, Inserted.ASY_PART_RVSN_ID, DELTEK.ITEM.ITEM_DESC,
Inserted.MODIFIED_BY, DELTEK.MI_CP_USER_NAME_LOOKUP.NAME, Inserted.TIME_STAMP
HAVING Inserted.MODIFIED_BY Not In ('C10863','M100133','M100196','M100273','M100212','DELTEK')
ORDER BY Inserted.ASY_PART_ID, Inserted.MODIFIED_BY;
SET @xml = CAST(( SELECT ASY_PART_ID AS 'td','', ASY_PART_RVSN_ID AS 'td','', ISNULL(ITEM_DESC,'') AS 'td','',
MODIFIED_BY AS 'td','', ISNULL(NAME,'') AS 'td','', TIME_STAMP AS 'td',''
From #Tmp a
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<html><body><H3>Trigger Report Mail</H3>
<table border = 1>
<tr>
<th> ASY_PART_ID </th> <th> ASY_PART_RVSN_ID </th> <th> ITEM_DESC </th>
<th> MODIFIED_BY </th> <th> NAME </th> <th> TIME_STAMP </th> </tr>'
SET @body = @body + @xml +'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL Mail', -- replace with your SQL Database Mail Profile
@body = @body,
@body_format ='HTML',
@recipients = 'wasay@gmail.com', -- replace with your email address
@subject = 'Email of Trigger Report on Table : DELTEK.MFG_BOM '
End
END
答案 0 :(得分:0)
我实际上会创建一个队列来发布所有电子邮件和一个SP来发送电子邮件。
请参阅SQL Server Service Broker(https://msdn.microsoft.com/en-us/library/ms190495.aspx),其中处理队列的SP是发送电子邮件的SP。这种方式更清晰,您应该能够更轻松地调试代码。
顺便说一句,在上面的代码中,您不需要创建#Tmp表。只需在第二个查询中附加第一个选择。