我创建了一个存储过程,如下所示,它将作为预定作业运行。存储过程的工作是通过考勤表(我在查询示例中省略)进行检查,并向员工及其经理发送有关缺席的电子邮件警报。
查询返回多行。
该过程正常运行没有任何问题,但只发送一次电子邮件,我相信我必须遍历检索到的数据。我尝试使用游标和临时表,但是遇到了很多错误而且它变得混乱了。
所以我在这里与工作存储过程分享,如果有人可以帮助我轻松有效地循环它,我将不胜感激。
感谢。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ABC]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @PositionCode NVARCHAR(9), @FullNameE NVARCHAR(50),
@Email NVARCHAR(100), @OutputSubject NVARCHAR(500),
@OutPutBody NVARCHAR(MAX), @OutPutEmail NVARCHAR(550),
@OutPutDateTime NVARCHAR(100)
DECLARE @CRLF char(2)
SELECT @CRLF = CHAR(13) + CHAR(10)
SELECT
@rno = ROW_NUMBER() OVER (ORDER BY ED.POsitionCode),
@FullNameE = EP.FullNameE, @Email = ED.Email
FROM
Query
BEGIN
SET @OutputSubject = 'Absent Employee - ' + @PositionCode + ' - ' + @FullNameE
SET @OutPutDateTime = (Select cast(GetDate() as datetime))
SET @OutPutBody = '<html><body>Dear Mr/Ms. ' + @FullNameE +
', <br><br> Please note that you were <b><font color="red">absent</font></b> during the <b><font color="red">last working day </font></b>: <br><br>Employee Code - '
+ @PositionCode + ' - ' + @FullNameE + ' </body></html>'
SET @OutPutEmail = @Email + '; abc@abc.com; xyz@xyz.com'
EXEC msdb.dbo.sp_send_dbmail
@recipients = @OutPutEmail,
@profile_name = 'Alert',
@subject = @OutputSubject,
@body_format ='HTML',
@body = @OutPutBody;
END
END