Send an email for each line in the result set

I would like to send an email for each row of the result set using sp_send_dbmail.

What is a suitable way to accomplish this without using loops?

Edit: I do not insist that the loop is not suitable here, but is there a set-based way. I tried to create a function, but the function cannot call the stored procedure inside it. Just another func or extended sp (which I wouldn't do either).

+3
source share
5 answers

The best way to achieve this is to put the email sending logic in a user-defined function.

SELECT MyEmailFunc (emailaddress) FROM MyTable

, , , . :

UDPATE MyTable SET SENT = MyEmailFunc ( ) = 0

0

- , ( ).

, , .

, .

.

( , ) , .

"" , , , , - -, .

(, ), , .

.

- , , , "" . .

+3

, :

"SendMails" Insert

sp_send_dbmail

:

Truncate Table SendMails

insert into SendMails (From, To, Subject,text) Select field1,field2,field3,field4 from MyTable
0

, . .

WHILE SQL, " "

BTW: ,

0

, , SQL Server: -D

SSRS - TSQL .

0
source

Source: https://habr.com/ru/post/1708767/


All Articles