Whatever message this page gives is out now! Go check it out!

QueryEach

Last update:
May 18, 2026
Note:
You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions .

Description

This function calls each row of the query provided.

Returns

None
Category
History
ColdFusion (2021 release): Introduced the following parameters:
  • parallel
  • maxThreadCount
ColdFusion (2016 release): Added the function.
See also

Syntax

queryEach(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])

Parameters

Parameter
Description
query
(Required) Query to be iterated over.
function
(Required) Function to be called with each row of the query.
parallel
(Optional) True if you want to enable parallel programming.
maxThreadCount
(Optional) The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

Example 1

<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32} 
                ]); 
   QueryEach(myQuery,function(any obj){
        writeOutput(obj.name & "<br/>")
   })
</cfscript>
Output
One
Two
Three

Example 2

<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
               
               function printQuery(any Obj){
                              WriteDump(Obj);
               }
               WriteOutput("QueryEach: ");
               QueryEach(sampleQuery, printQuery);
</cfscript>
The script loops through each item in the query.

Using member function

<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       // Execute the function to call each row and return the values of Location
       myResult.each(function(count){
             myList=#count.LOCATION#;
             WriteOutput(myList);
       });
</cfscript>

Using parallelization- Example 1

<cfscript> 
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",  
                [  
                        {id=1,name="One",amount=15},  
                        {id=2,name="Two",amount=18},  
                        {id=3,name="Three",amount=32} , 
      {id=4,name="Three",amount=32}, 
      {id=5,name="Three",amount=32}, 
      {id=6,name="Three",amount=32}, 
      {id=7,name="Three",amount=32}, 
      {id=8,name="Three",amount=32}, 
      {id=9,name="Three",amount=32}, 
      {id=10,name="Three",amount=32}, 
      {id=11,name="Three",amount=32} 
       
                ]);  
    function callbck(any obj){ 
        writeOutput(obj.name & "<br>") 
   } 
   QueryEach(myQuery,callbck,true,10) 
</cfscript> 
 
<cfscript> 
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; 
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); 
                 
               function printQuery(any Obj){ 
                              Writeoutput(Obj.bookimage & "<br>"); 
               } 
               WriteOutput("QueryEach: "); 
               QueryEach(query =sampleQuery,callback= printQuery,parallel=false,maxthreadcount=5); 
      QueryEach(query =sampleQuery,callback= printQuery,parallel=false); 
</cfscript> 
 
<cfscript> 
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); 
       // Execute the function to call each row and return the values of Location 
       myResult.each(function(count){ 
             myList=#count.LOCATION#; 
             WriteOutput(myList & "<br>"); 
       },true,10); 
</cfscript>

Using parallelization- Example 2

<cfscript> 
    // Install the package derby before executing this code. cfpm>install derby 
    qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; 
    sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); 
                     
    function printQuery(any Obj){ 
        return Obj; 
    } 
    t_start=GetTickCount() 
    QueryEach(query =sampleQuery,callback= printQuery,parallel=true,maxthreadcount=50) 
    t_end=GetTickCount() 
    writeoutput("<br>Time taken with 50 threads:" &  t_end-t_start) 
</cfscript>

Using parallelization- Example 3

<cfscript> 
    myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); 
    // Execute the function to call each row and return the values of Location 
    t_start=GetTickCount() 
        myResult.each(function(count){ 
                  myList=#count.LOCATION#; 
                  WriteOutput(myList); 
        },true,50) 
    t_end=GetTickCount() 
    writeOutput("<br>Time taken with 50 threads:" &  t_end-t_start) 
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page