每个结果行执行 SQL
我已经在谷歌上搜索了一段时间,但似乎找不到这个问题的答案。我试图在存储过程中使用 SQL Server 2005 中的数据库邮件,其中的想法是为查询中的每一行发送单独的邮件,每封邮件取决于行中驻留的地址。
例如,假设有 15 - 50 行产品订单,其中包含以下数据:ID、CustomerName、CustomerEmail、ProductName、ProductCategory。每行包含客户电子邮件以及产品类别。我需要使用特定于自己行的个人数据向每个客户发送确认电子邮件,以及根据产品类别向不同公司人员发送实际订单邮件。
如何像这样动态发送数据库邮件?
提前致谢!
编辑1:数据集等不是一个选项,这必须专门在SQL Server 2005上运行。我想我必须研究游标。
I've been googling around for a bit but I can't seem to find an answer to this. I'm trying to use the Database Mail in SQL Server 2005 in a stored procedure where the idea is to send separate mail for each row in a query, each mail depending on an address residing in the rows.
Imagine for example 15 - 50 rows of product orders with the following data: ID, CustomerName, CustomerEmail, ProductName, ProductCategory. Each of the rows contains the customer email as well as the product category. I need to send a confirmation email to each of those customers using their personal data specific to their own rows, as well as the actual order mail to different company people depending on the product category.
How can I send Database Mail dynamically like this?
Thanks in advance!
Edit 1: Datasets etc are not an option, this has to be run exclusively on SQL Server 2005. I suppose I'll have to look into cursors then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这是不可能的。发送数据库邮件是对存储过程的调用,这在查询或函数中都是不允许的。
您必须将每封邮件一一发送。您可以在存储过程中使用服务器上的游标或迭代客户端中的表。
No, is not possible. Sending database mail is an invocation of a stored procedure and this is not allowed in queries, nor in functions.
You must send each mail one by one. You can use a cursor on the server in a stored procedure or iterate the table in the client.
使用 DataSet 将表放入内存
然后运行 foreach DataRow ,获取邮件并发送......
Get the Table into the memory using a DataSet
and than run foreach DataRow , get the mail and send it...
您的意思是每次添加新行(例如下订单)?如果是这样,您可以使用触发器每次发送电子邮件插入或更新行。
Do you mean each time a new row is added (e.g. order is placed)? If so, you can use a Trigger to send the email each time a row is inserted or updated.