How to loop through a SQL query and output individual emails base on

187 views Asked by At

I'm trying to loop over information from a query and send that information in an email. Currently, based on my stored procedure, I'm displaying all the rows in the email.

This is what I'm using to get the above information:

<table>
  <thead>
    <tr>
      <th scope="col" id="left">Admin Name</th>
      <th scope="col" id="middle">Department Name</th>
      <th scope="col" id="right">Last Logon</th>
    </tr>
  </thead>
  <tbody>
    <cfloop query="#inactiveAdmins#">                
      <tr>
        <td class="text-left">#Admin_Name#</td>
        <td class="text-left">#Dept_Name#</td>
        <td class="">#(Len(Last_Logon) ? dateFormat(Last_Logon, 'mmm dd, yyyy') : 'Never Logged On')#</td>
      </tr>
    </cfloop>
  </tbody>
</table>

This is displaying all Admin Names, All Department Names and all Last Logon.

I need to be able to loop over each department and send an email to each department individually.

To loop over each department, this is what I'm trying, but it's not returning any results. My question is:

Is the syntax correct?

<cfloop query="#ALEmail#">
  <cfquery dbtype="query" name="inactiveSW">
    SELECT Dept_ID
    FROM inactiveSW
    WHERE Dept_ID = <cfqueryparam cfsqltype="cf_sql_char" value="#ALEmail.Dept_ID#">
  </cfquery>
</cfloop>
1

There are 1 answers

1
James A Mohler On BEST ANSWER

This is more of a comment than an answer, but it is long

should be

On this part

  <cfquery dbtype="query" name="inactiveSW">
    SELECT Dept_ID
    FROM inactiveSW
    WHERE Dept_ID = <cfqueryparam cfsqltype="cf_sql_char" value="#ALEmail.Dept_ID#">
  </cfquery>

Because the FROM is the same as the name=, it is like to either have a syntax error, or overwrite an existing variable.

Besides, you are just selecting a variable that already exists. This doesn't get any new information. Are you trying to test for existence of dept_id?


Lastly, if you are trying to send an email based on a query, it is really straight forward

<cfmail
query="ALEmail"
from="#from#"
to="#to#"
subject="#subject#">


     Content here


</cfmail>