Parameter
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.
This function calls each row of the query provided.
None
Category
History
ColdFusion (2021 release): Introduced the following parameters:
ColdFusion (2016 release): Added the function.
See also
queryEach(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])
|
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. |
<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
<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.
<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>
<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>
<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>
<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>
Sign in to your account