2
我曾經在我的數據庫中使用觸發器,它使用cursors
/sp_send_dbmail
在某些列更新時發送電子郵件。我被告知這不是最佳實踐,所以我創建了一個名爲EmailNotify的新表,其中包含像recepient,主題,正文等列。因此,觸發器現在將插入此表中的我要發送的電子郵件。從自定義電子郵件表發送電子郵件的代理作業
我想創建一個每隔幾分鐘運行一次的作業,檢查此表和電子郵件。下面的項目是我想出來的,但在這種情況下使用遊標可以嗎?該表應該包含發送的字段,以便知道我發送了哪些行?我可以在光標內改變它嗎?或者是否會建議事後截斷表格?
DECLARE @emailSubject AS NVARCHAR(100);
DECLARE @emailRecipients AS NVARCHAR(100);
DECLARE @emailBody AS NVARCHAR(max);
DECLARE cur CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
SELECT
recipients,
subject,
body
FROM
EmailNotify;
OPEN cur
FETCH NEXT FROM cur INTO
@emailRecipients,
@emailSubject,
@emailBody
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'name',
@recipients = @emailRecipients,
@body = @emailBody,
@body_format = 'HTML',
@subject = @emailSubject;
FETCH NEXT FROM cur INTO
@emailRecipients,
@emailSubject,
@emailBody
END
CLOSE cur
DEALLOCATE cur
如果你喜歡這類問題/答案,那麼我敢打賭你會喜歡[CodeReview](http://codereview.stackexchange.com/help/on-topic):) –