我有一个查询,检查是否已经销售一个特定的库存项目
select * from merchand_history where stock_code = 'zzz007' and create_timestamp >= getdate() order by create_timestamp desc
我想有一个电子邮件用户(我想使用警报机制)的SQL作业,但只有当该查询返回的行。
我无法想象如何做到这一点,并顺从投票。 我真的需要一个SQL唯一的解决scheme…
尝试构build一个如下所示的存储过程,并安排它作为一个工作来运行:
create procedure [dbo].[sp_send_merchant_email] as Begin declare @recordCount int select @recordCount = isnull(count(*), 0) from merchand_history where stock_code = 'zzz007' and create_timestamp >= getdate() order by create_timestamp desc IF (@recordCount > 0) begin EXEC msdb.dbo.sp_send_dbmail @profile_name = 'YourProfile', @recipients = '[email protected]', @query = 'select * from merchand_history where stock_code = ''zzz007'' and create_timestamp >= getdate() order by create_timestamp desc' , @subject = 'Merchant Email ', @Body = 'Email Merchant..... ' , @attach_query_result_as_file = 1 ; End else begin EXEC msdb.dbo.sp_send_dbmail @profile_name = 'YourProfile', @recipients = '[email protected]', @BODY = 'No data returned ', @subject = 'Merchant Email' End End;