Send mail for each row in a result set

  • I want to send one mail to each row in the result query.

    My first query select * from mailstatus Where status = 'Fail'

    Retrive 3 rows . I attached copy of that here.

    For each row i want to execute one store procedure sp_SQLNotify

    Which sending mail using CDO object.

    I am passing following parameter

    @From varchar(500) ,

    @To varchar(500) ,

    @Subject varchar(500),

    @Body varchar(4000) ,

    to store procedure which are the column value of the 1st query result.

    How can i do these. Please help me.

  • You can use cursor to loop through mailstatus table and execute sp_SQLNotify from every result.

    like

    declare @username varhcar(30)

    declare c cursor local fast_forward for

    select username from mailstatus Where status = 'Fail'

    open c

    fetch next from c into @username

    while @@fetch_status = 0

    begin

    exec sp_SQLNotify 'MyMail@mail.com', @username, 'Subject', 'Body'

    fetch next from c into @username

    end

    close c

    deallocate c

  • Hi simon

    Thank for your quick response. Its really help me.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply