我有一个查询,检查我们是否已经销售了某个特定的库存商品
select * from merchand_history where stock_code = 'zzz007' and create_timestamp >= getdate() order by create_timestamp desc
我想要一个向用户发送电子邮件的 SQL 作业(我猜是使用警报机制),但前提是该查询返回了行。
我想不出如何做到这一点并提交给 HiveMind。我真的需要一个仅使用 SQL 的解决方案……
答案1
尝试构建如下所示的存储过程并安排它作为作业运行:
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;