Name
Apart from the generic script syntax for the tag, there is another script alternative to cfquery tag, provided by the QueryExecute function. This function simplifies the query execution in the CFScript block.
This function lets you pass unnamed parameters to the query. Use '?' (question mark) as the place-holder for the parameters. The values to the parameters should be passed as an array toparams.
Note that the following points are applicable:
The Query tag is also available as a ColdFusion function apart from the generic script syntax for the CFQuery tag. This function simplifies the query execution in the CFScript block.
QueryExecute(sql,params,options)
Name |
Required |
Description |
sql |
Yes |
SQL string to execute. |
params |
No |
Array or Struct of parameter values. |
options |
No |
Struct containing query options. Values are:
For a list of supported queryOptions, see the documentation for <cfquery>. All except the name option are supported. |
ColdFusion (2021 release): Added the attribute returnType, with the following return types:
ColdFusion (2018 release): Added the value disableAutoGenKeys in options.
ColdFusion 11: Added this function
Example 1
<cfset qoptions = {result="myresult", datasource="artGallery", fetchclientinfo="yes"}> <cfset myquery = QueryExecute("select * from art where ARTID < 5", [] ,qoptions)> <cfdump var="#myQuery#" > <cfset myquery1 = QueryExecute("select * from art where ARTID < ?", [4] ,qoptions)> <cfdump var="#myQuery1#" > <cfset myquery2 = QueryExecute("select * from art where ARTID < :artid and artistid=:aid ", {artid={value=100}, aid=2} ,qoptions)> <cfdump var="#myQuery2#" >
Example 2
QueryExecute ("select from Employees where empid=1"); QueryExecute("select from Employee where country=:country and citizenship=:country", {country='USA'}); QueryExecute("select from Employee where country=:country and citizenship=:country", {country={value='USA', CFSQLType='CF_SQL_CLOB', list=true, other‐queryParam_attributes}, }); QueryExecute("select from Employee where country=:country and citizenship=:country", {country='USA'}, {datasource=”cfartgallery”, cachename=”employees”}) QueryExecute ("select from Artists where artistid=? and country=?", [1, “USA”], {datasource="cfartgallery"});
The example searches for all employees whose emp id is between 4-14 and displays the results.
<cfscript> myResult=QueryExecute(("SELECT * FROM EMPLOYEES WHERE EMP_ID BETWEEN :low AND :high"),{low=4,high=14}, {datasource="cfdocexamples"}); WriteDump(myResult); </cfscript>
Output
Example 4
This example searches for records in the table for a particular id and name.
<cfscript> myResult=QueryExecute("SELECT * FROM MYEMPLOYEES WHERE ID=:ID AND Name=:NAME",{ID="004",Name="Jill"}, {datasource="cfdocexamples"}); WriteDump(myResult); </cfscript>
Output
Example 5
This example searches for all employees working in sales and stays in either Newton or San Francisco.
<cfscript> myResult=QueryExecute(("SELECT * FROM EMPLOYEES WHERE DEPARTMENT='Sales' AND LOCATION in (?,?)"),["Newton","San Francisco"], {datasource="cfdocexamples"}); WriteDump(myResult); </cfscript>
Output
The sample below used the parameter disableAutoGenKeys.
<cfscript> q=QueryExecute("Select * from art where artid=1",{}{datasource="cfartgallery", disableAutoGenKeys="false"} ); writedump(q); </cfscript>
<cfscript> cfhttp(name="myQuery", url="https://raw.githubusercontent.com/sauravg94/test-repo/master/MOCK_DATA.csv", firstrowasheaders="true", method="GET"); myResult=QueryExecute(("SELECT * from myQuery WHERE id BETWEEN :low AND :high"),{low=100,high=500},{dbtype="query"}) writeDump(myResult) </cfscript>
<cfscript> // QueryExecute return type array qoptions = {result="myresult", datasource="cfartgallery",returntype="array"} myquery = QueryExecute("select ARTID,ARTISTID,ARTNAME from art where ARTID < 2", [] ,qoptions); writeOutput(myquery[1].ARTID &" ") writeOutput(myquery[1].ARTISTID &" ") writeOutput(myquery[1].ARTNAME &" ") </cfscript>
<cfscript> // QueryExecute return type "json/array" qoptions = {result="myresult", datasource="cfartgallery",returntype="json/array"} myquery = QueryExecute("select ARTID,ARTISTID,ARTNAME from art where ARTID < 2", [] ,qoptions); record=deserializeJSON(myquery) writeOutput(record[1].ARTID &" ") writeOutput(record[1].ARTISTID &" ") writeOutput(record[1].ARTNAME &" ") writeOutput(myquery) </cfscript>
<cfscript> // QueryExecute return type default query qoptions = {result="myresult", datasource="cfartgallery"} myquery = QueryExecute("select ARTID,ARTISTID,ARTNAME from art where ARTID < 2", [] ,qoptions); record=QueryGetRow(myQuery,1) writeOutput(record.ARTID &" ") writeOutput(record.ARTISTID &" ") writeOutput(record.ARTNAME &" ") </cfscript>
Sign in to your account