加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server-2005 – 仅当查询包含要返回的行时才发送sql server

发布时间:2020-12-12 17:00:27 所属栏目:MsSql教程 来源:网络整理
导读:我有一个查询,检查我们是否已销售特定库存商品 select * from merchand_history where stock_code = 'zzz007' and create_timestamp = getdate() order by create_timestamp desc 我想有一个sql作业通过电子邮件发送给用户(我想使用警报机制),但前提是该查询
我有一个查询,检查我们是否已销售特定库存商品
select * from merchand_history where stock_code = 'zzz007' and create_timestamp >= getdate() order by create_timestamp desc

我想有一个sql作业通过电子邮件发送给用户(我想使用警报机制),但前提是该查询返回了行.

我无法想到如何做到这一点并服从于hivemind.我真的需要一个只有sql的解决方案……

解决方法

尝试构建类似下面的存储过程并安排它作为工作运行:
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 = 'recipients@yourcompany.com',@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',@BODY = 'No data returned ',@subject = 'Merchant Email'

End
End;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读