如何循环 SQL 查询并根据

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

我正在尝试循环查询来自查询的信息并在电子邮件中发送该信息。目前,根据我的存储过程,我正在显示电子邮件中的所有行。

这是我用来获取上述信息的方法:

<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>

这显示了所有 管理员名称 、所有 部门名称 和所有 上次登录 .

我需要能够遍历每个部门并分别向每个部门发送电子邮件。

要遍历每个部门,这就是我正在尝试的方法,但它没有返回任何结果。我的问题是:

语法正确吗?

<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>

这与其说是一个答案,不如说是一个评论,但是它很长

应该是

这部分

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

因为FROMname=一样,好像是语法错误,或者覆盖了已有的变量。

此外,您只是选择了一个已经存在的变量。这没有得到任何新信息。您是否要测试 dept_id 是否存在?


最后,如果您尝试根据查询发送电子邮件,那真的很简单

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


     Content here


</cfmail>