<html>
<head>
<link href="mmres://user_interface_reference.css" rel="stylesheet" type="text/css">
<title>CFLOOP: LOOPING OVER A QUERY</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
   <div id="Description">
   <table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
      <tr> 
         <td valign="top" class="name">CFLOOP: LOOPING OVER A QUERY</td>
         <td valign="top" nowrap class="compatibility">&nbsp;</td>
      </tr>
      <tr>
         <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
      </tr>


    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Description</span>
<p>
A loop over a query executes for each record in a query record set. The results are similar to those of the cfoutput tag. During each iteration, the columns of the current row are available for output. The cfloop tag loops over tags that cannot be used within a cfoutput tag.
</p>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="syntax"><span class="title">Syntax</span><pre>&lt;cfloop 
   query = &quot;query_name&quot;
   startRow = &quot;row_num&quot;
   endRow = &quot;row_num&quot;&gt;
&lt;/cfloop&gt;
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">See also</span>
<p>
cfabort, cfbreak, cfexecute, cfexit, cfif, cflocation, cfoutput, cfswitch, cfthrow, cftry; For more information, see "cfloop and cfbreak" in Chapter&#160;2, "Elements of CFML," in ColdFusion MX Developer's Guide
</p>

<p>

</p>
    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Example</span><pre>&lt;cfquery name = &quot;MessageRecords&quot; 
dataSource = &quot;cfdocexamples&quot;&gt; 
SELECT * FROM Messages 
&lt;/cfquery&gt;
&lt;cfloop query = &quot;MessageRecords&quot;&gt; 
&lt;cfoutput&gt;#Message_ID#&lt;/cfoutput&gt;&lt;br&gt;
&lt;/cfloop&gt;
</pre>
<p>
The cfloop tag also iterates over a record set with dynamic start and stop points. This gets the next n sets of records from a query. This example loops from the fifth through the tenth record returned by the MessageRecords query:
</p>
<pre>&lt;cfset Start = 5&gt; 
&lt;cfset End = 10&gt; 
&lt;cfloop query = &quot;MessageRecords&quot; 
startRow = &quot;#Start#&quot; 
endRow = &quot;#End#&quot;&gt; 
&lt;cfoutput&gt;#MessageRecords.Message_ID#&lt;/cfoutput&gt;&lt;br&gt;
&lt;/cfloop&gt;
</pre>
<p>
The loop stops when there are no more records, or when the current record index is greater than the value of the endRow attribute. 
</p>

<p>
The following example combines the pages that are returned by a query of a list of page names into one document, using the cfinclude tag: 
</p>
<pre>&lt;cfquery name = &quot;GetTemplate&quot; 
dataSource = &quot;Library&quot;
maxRows = &quot;5&quot;&gt; 
SELECT TemplateName 
FROM Templates 
&lt;/cfquery&gt; 
&lt;cfloop query = &quot;GetTemplate&quot;&gt; 
&lt;cfinclude template = &quot;#TemplateName#&quot;&gt; 
&lt;/cfloop&gt; 
</pre>
         </td>
      </tr>
   </table>
   </div>
<div id="QUERY">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>  
<td valign="top" class="name">QUERY</td>
  <td valign="top" nowrap class="compatibility">&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
  </tr>

  <tr>
  <td valign="top" class="syntax">&nbsp;</td>
  <td valign="top" nowrap class="requirements">Required</td>
  </tr>
  <tr>
  <td colspan="2" class="clearseparation">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top" colspan="2" class="description">



<p>Query that controls the loop. </p>

  </td>
  </tr>
  </table>
</div>
<div id="STARTROW">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>  
<td valign="top" class="name">STARTROW</td>
  <td valign="top" nowrap class="compatibility">&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
  </tr>

  <tr>
  <td valign="top" class="syntax">&nbsp;</td>
  <td valign="top" nowrap class="requirements">Optional</td>
  </tr>
  <tr>
  <td colspan="2" class="clearseparation">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top" colspan="2" class="description">



<p>First row of query that is included in the loop. </p>

  </td>
  </tr>
  </table>
</div>
<div id="ENDROW">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>  
<td valign="top" class="name">ENDROW</td>
  <td valign="top" nowrap class="compatibility">&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
  </tr>

  <tr>
  <td valign="top" class="syntax">&nbsp;</td>
  <td valign="top" nowrap class="requirements">Optional</td>
  </tr>
  <tr>
  <td colspan="2" class="clearseparation">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top" colspan="2" class="description">



<p>Last row of query that is included in the loop. </p>

  </td>
  </tr>
  </table>
</div>

  </body>
</html>
