title: How to "iterate" through a SQL result in stored procedure but avoid using a cursor?
body: <p>I am fairly new to doing more involved SQL work.</p>
  
  <p>My goal is to email using the results of a query to drive email creation. I intend to create a stored procedure and schedule this twice a week (there will be at most 20 emails, this will not be heavy email load) on SQL Server 2008.</p>
  
  <pre><code>SELECT ProjectCodes.ProjectCode, COUNT(Projects.ProjectsID),  ProjectApprovers.EmailApprover
  FROM Projects 
  INNER JOIN ProjectCodes
      ON Projects.ProjectCodesID=ProjectCodes.ProjectCodesID
  INNER JOIN ProjectApprovers
      ON Projects.ProjectCodesID=ProjectApprovers.ProjectCodesID
  WHERE ProjectApprovers.IsPrimaryApprover=1
  group by ProjectCodes.ProjectCode, ProjectApprovers.EmailApprover
  </code></pre>
  
  <p>This returns something similar to:</p>
  
  <pre><code>+-------------+-------+--------------+
  | ProjectCode | Count | EmailAddress |
  +-------------+-------+--------------+
  | Code1       |     4 | Email1       |
  | Code2       |     2 | Email2       |
  | Code3       |     2 | Email3       |
  | Code4       |     3 | Email4       |
  +-------------+-------+--------------+
  </code></pre>
  
  <p>What I would like to do is basically loop through this result, running the following:</p>
  
  <pre><code>EXEC msdb.dbo.sp_send_dbmail
  @recipients= 'email1',     --email address from above query
  @subject='Test email',
  @body='You have X projects waiting'    -- where X is the COUNT() term
  </code></pre>
  
  <p>for each of the rows.</p>
  
  <p>My understanding is I could do this somewhat straightforward for each entry if I use a cursor, but, all the documentation and Stack Overflow results I've found strongly suggest this is not a good strategy.</p>
  
  <p>What is the best way to do something like this?</p>