为结果集中的每一行发送电子邮件 [英] Send email for each row in a result set
问题描述
我想使用 sp_send_dbmail 为结果集的每一行发送一封电子邮件.
I'd like to send an email for each row of a result set using sp_send_dbmail.
在不使用循环的情况下完成此任务的适当方法是什么?
What is the appropriate way to accomplish this without using loops?
我不是坚持循环在这里不合适,但是否有基于集合的方法来做到这一点.我试过创建一个函数,但函数不能调用其中的存储过程.只有另一个 func 或扩展 sp(我宁愿不这样做).
I'm not insisting a loop is not proper here, but is there a set based way to do this. I've tried creating a function, but a function cannot call a stored proc inside it. Only another func or extended sp (which I'd rather not do either).
推荐答案
实现此目的的最佳方法是将您的电子邮件发送逻辑置于用户定义的函数中.
The best way to accomplish this is to put your email sending logic in a user defined function.
然后你只需调用 SELECT MyEmailFunc(emailaddress) FROM MyTable
Then you would simply call SELECT MyEmailFunc(emailaddress) FROM MyTable
它避免了循环,您甚至可以在更新语句中使用它来显示电子邮件已发送.例如:
It avoids loops and you can even use it in an update statement to show that the email was sent. For example:
UDPATE MyTable SET SENT = MyEmailFunc(emailaddress) WHERE sent = 0
UDPATE MyTable SET SENT = MyEmailFunc(emailaddress) WHERE sent = 0
这篇关于为结果集中的每一行发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!