我在运行SQL Server作业时遇到问题。我有一份工作,如果他们不提交报告,会将剩余邮件发送到表格中的人员列表。以下查询我用来每隔一小时向所有人发送电子邮件。问题是,他们每小时都会收到重复的邮件。例如,如果工作从下午3点开始,他们将获得一个剩余时间,下午4点他们将获得2个剩余时间和下午5点3个剩余的重复电子邮件。我的意思是同时发送三封电子邮件。有谁可以帮我确定我在做错误的地方?
DECLARE @count int
DECLARE @Recepient_Email varchar(500)
DECLARE @P_Name varchar(500)
SET @count = 1
while (@count <=(select COUNT(ID) from Email_Check))
BEGIN
select @Recepient_Email=Email_Check.Email_Address, @P_Name=Email_Check.P_Name
from Email_Check
inner join W_P_Tickets
on Email_Check.P_Name = W_P_Tickets.P_Name
where Email_Check.ID = @count and W_P_Tickets.PP_Attachment is Null
set @P_Name += ' - WSR Remainder'
EXEC msdb.dbo.sp_send_dbmail
@profile_name='RDB',
@recipients=@Recepient_Email,
@subject = @P_Name,
@body_format = 'HTML',
@body = '<html><body><p>Team, <br><br>Please send report ASAP<br><br>Thanks <br>Team Name</p></body></html>'
set @count =@count +1
END