Coldfusion is old but not dead yet, so it is not surprise if you still need to work on them from time to time. I recently changed my job and encountered some web apps coded with ColdFusion.
Today I need to put a cfquery into a function so the query can be reused. Here is the solution I came up with.
<cffunction name="myQuery" access="private"> <cfargument name="condition" required="yes" type="string"> <cfquery name="expiryQuery" datasource="myDb"> select count(id) AS expired from myTable where expiryDate<= getDate() and #arguments.condition# </cfquery> <cfreturn myQuery> </cffunction>
Please note that the code above is for demonstration purpose. To prevent SQL injection attack, you need to use cfqueryparam to qualify the query parameters.
Now you can invoke the function and assign the query result to a variable for later access
<cfset result1=#expiryCheck("flag = 1")# > <cfset result2=#expiryCheck("flag = 2")# >