Search code examples
coldfusioncoldfusion-9cfquery

Is it possible to have dynamically generated query names in ColdFusion?


What I am trying to do is

<cfloop array="#LOCAL.someArray" index="LOCAL.aString">

    <cfset LOCAL.queryName = "uniqueQueryName_" & LOCAL.aString />

    <cfquery name="#LOCAL.queryName#" datasource="db" cachedwithin="#CreateTimeSpan(1,0,0,0)#">
        SELECT count(*) AS c FROM someTable
    </cfquery>

    <cfdump var="#LOCAL.queryName#" />
</cfloop>

is this possible, or is there a better way to do it?

Edit

This works with <cfloop query="LOCAL.queryName"> but not when I try to do <cfset ArrayAppend(LOCAL.returnArray, LOCAL.queryName.c) />


Solution

  • There is no need to use evaluate() to do this, and one shouldn't (so I've down-voted that answer, sorry).

    All you need to do is use associative array notation:

    <cfdump var="#local[qname]#">
    

    If one wants to access a column of that query, it's:

    #local[qname][columnName]#
    

    And for a specific cell:

    #local[qname][columnName][rowNumber]#
    

    There are very very very few situations in which evaluate() is the correct answer to anything. One cannot rely on the Adobe docs because - unfortunately - an awful lot of it was not written by very experienced ColdFusion developers.