Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8109365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:24:02+00:00 2026-06-06T01:24:02+00:00

I’m not sure when it is appropriate to use a cursor. I need to

  • 0

I’m not sure when it is appropriate to use a cursor.

I need to send out emails after a certain stored procedure is ran that updates some tables. So if

table1.field1 = "value" and 
   (select count(*) 
      from table1 join table2 
        on table2.table1_id = table1.id) > 20 

then an email needs to be sent out for everything in table1 that meets those conditions. And in the email I need to include table1.field2 and table2.field3 and so on…

The only way I can think to do this is get my dataset of rows in table1 that meet these conditions and then use a cursor to go through it. I’ve never worked with sending emails from SQL Server before and I don’t know my options… I have never used a cursor before (although I do understand how) because I learned to shy away from them.

Thanks.

Edit:
With the while loop: So… I probably want to shove all the info i need into a temp table (select * from table1 where (conditions)) and then start a while loop that goes through each row of the resulting data set in my temp table. (WHILE i = 1 to (select count(*) from #temp) / assemble e-mail send e-mail / next)… then what is the best way to reference/point to which row of #temp I should be using for each loop iteration? I could just give #temp an identity(1,1) or use row_number()… right? I guess? It seems like I’m just trying to program my own cursor by doing this, though? And let’s say we set @subject = (select ‘Site ‘ & field2 & ‘ has a value of ‘ & field1 from #temp where temp_id_or_row_number = @i) and then increment @i for each loop…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:24:05+00:00Added an answer on June 6, 2026 at 1:24 am

    I have used something similar to this article
    I used to build up straight html and then use that in an html formatted mail using sp_send_dbmail.
    These stored procedures can get messy

    I never used cursors – had several stored procs which had WHILE loops sending out multiple emails

    WHILE (condition)
       begin
       end
    

    Here’s a full example; hope it helps:

    /*
    drop table #the_table
    drop table #Emails
    */
    create table #the_table 
    ( [Email] varchar(50),
      [Date] datetime,
      [Amount] decimal(12, 2))
    
    insert into #the_table
    values
    ( 'example@googlemail.co.uk', '2012-1-1', 10),
    ( 'example@googlemail.co.uk', '2012-1-3', 10),
    ( 'foo@bar.co.uk', '2012-1-3', 20),
    ( 'foo@bar.co.uk', '2012-1-5', 10)
    
    
      --get a list of email addresses
    SELECT   
        Email
        , ROW_NUMBER() OVER(ORDER BY [Email]) AS [Counter] 
    INTO    #Emails
    FROM    #the_table
    GROUP BY Email
    
    
    DECLARE @html VARCHAR(8000)
    DECLARE @Recipient VARCHAR(100)
    
    DECLARE @row INT = 1
    
    WHILE @row <= (SELECT COUNT(*) FROM #Emails)
    BEGIN
        SET @Recipient= (SELECT [Email] FROM #Emails WHERE [Counter] = @row)
    
    
          --start building the html string
        SELECT @html = '<html><p>Hello World.</p>'
        SELECT @html = @html + '<p><table border="1"><tr><th>Date</th><th>Amount</th></tr>' 
    
        SELECT
            @html = 
                @html + 
                ('<tr><td>' + CONVERT(VARCHAR(6), [Date])                           
                + '</td><td>$' + LEFT(CONVERT(VARCHAR(20), CONVERT(MONEY,Amount),1), LEN(CONVERT(VARCHAR(20), CONVERT(MONEY,Amount),1))-3) + '</td></tr>')
        FROM    #the_table
        WHERE   [Email] = @Recipient
        SELECT @html = @html + '</table></p>'   
    
          --finish building the html string 
        SELECT @html = @html + '<p></p></html>'
    
    
        DECLARE @mySubject VARCHAR(100)
        SET @mySubject = 'TESTING 1-2-3'
    
        EXEC msdb..sp_send_dbmail
            @recipients = @Recipient  
            , @subject = @mySubject
            , @body_format = 'html'
            , @body = @html 
    
    
        SET @Row = @Row +1
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.