对于您的帮助,请按照以下步骤操作,它会向我发送带有贷款的电子邮件,该电子邮件将在当前日期后2个月后终止,如果没有结果,它仍然会向我发送电子邮件说没有受影响的行
所有我需要的只是在有结果时接收电子邮件,如果没有结果就不发送电子邮件
ALTER PROCEDURE [LoanEndByTwoMonths_SP]
-- Add the parameters for the stored procedure here
AS
BEGIN
SET NOCOUNT ON;
IF
(
SELECT COUNT(LoanID)
FROM ELSBassel.Loan.Loans
) > 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ELS',
@recipients = 'Bassel.Mahmoud@test.com',
@query = 'SELECT LoanStartDate,
LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1 AS LoanEndDate,
[FinanceSourceID]
FROM ELSBassel.Loan.Loans
WHERE(LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1) = (DATEADD(MONTH, 2, GETDATE()))
ORDER BY LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1 DESC;',
@execute_query_database = 'ELSBassel',
@subject = 'Loan will end after two months';
COMMIT;
END;
END;
GO
答案 0 :(得分:6)
我认为,只需在WHERE子句中添加相同的WHERE条件即可解决您的问题:
ALTER PROCEDURE [LoanEndByTwoMonths_SP]
-- Add the parameters for the stored procedure here
AS
BEGIN
SET NOCOUNT ON;
IF
(
SELECT COUNT(LoanID)
FROM ELSBassel.Loan.Loans
WHERE (LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1)
= (DATEADD(MONTH, 2, GETDATE()))
) > 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ELS',
@recipients = 'Bassel.Mahmoud@test.com',
@query = 'SELECT LoanStartDate,
LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1 AS LoanEndDate,
[FinanceSourceID]
FROM ELSBassel.Loan.Loans
WHERE(LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1) = (DATEADD(MONTH, 2, GETDATE()))
ORDER BY LoanStartDate + DATEADD(MONTH, AvailabilityPMonth, 0) - 1 DESC;',
@execute_query_database = 'ELSBassel',
@subject = 'Loan will end after two months';
COMMIT;
END;
END;
GO