– Common Function Library Project

queryExecute(sql_statement[, queryParams][, queryOptions])

Last updated September 22, 2014


Henry Ho

Version: 1 | Requires: CF9 | Library: CFMLLib

Include a cfm with this function if server.coldfusion.productVersion is 9 or 10.

Return Values:
Returns a query.


QueryExecute ("select from Employees where empid=1");
QueryExecute("select from Employee where country=:country and citizenship=:country", {country='USA'});
More example:


Name Description Required
sql_statement SQL. Yes
queryParams Struct of query param values. No
queryOptions Query options. No

Full UDF Source:

 Backport of QueryExecute in CF11 to CF9 &amp; CF10
 @param sql_statement 	 SQL. (Required)
 @param queryParams 	 Struct of query param values. (Optional)
 @param queryOptions 	 Query options. (Optional)
 @return Returns a query. 
 @author Henry Ho ( 
 @version 1, September 22, 2014 
<cffunction name="QueryExecute" output="false"
				* result struct is returned to the caller by utilizing URL scope (no prefix needed) *">
	<cfargument name="sql_statement" required="true">
	<cfargument name="queryParams"  default="#structNew()#">
	<cfargument name="queryOptions" default="#structNew()#">
	<cfset var parameters = []>
	<cfif isArray(queryParams)>
		<cfloop array="#queryParams#" index="local.param">
			<cfif isSimpleValue(param)>
				<cfset arrayAppend(parameters, {value=param})>
				<cfset arrayAppend(parameters, param)>
	<cfelseif isStruct(queryParams)>
		<cfloop collection="#queryParams#" item="local.key">
			<cfif isSimpleValue(queryParams[key])>
				<cfset arrayAppend(parameters, {name=local.key, value=queryParams[key]})>
				<cfset var parameter = {name=key}>
				<cfset structAppend(parameter, queryParams[key])>
				<cfset arrayAppend(parameters, parameter)>
		<cfthrow message="unexpected type for queryParams">
	<cfif structKeyExists(queryOptions, "result")>
		<!--- strip scope, not supported --->
		<cfset queryOptions.result = listLast(queryOptions.result, '.')>
	<cfset var executeResult = new Query(sql=sql_statement, parameters=parameters, argumentCollection=queryOptions).execute()>
	<cfif structKeyExists(queryOptions, "result")>
		<!--- workaround for passing result struct value out to the caller by utilizing URL scope (no prefix needed) --->
		<cfset URL[queryOptions.result] = executeResult.getPrefix()>
	<cfreturn executeResult.getResult()>
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson