Is it possible to have dynamically generated query

2019-07-10 08:14发布

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) />

2条回答
太酷不给撩
2楼-- · 2019-07-10 08:27

You can dump the query, and I imagine also access it by doing something like this:

<cfloop list="q1,q2,q3" index="qname">


    <cfquery name="#qname#" datasource="dsn">
        SELECT * from some_table;
    </cfquery>

    <cfdump var="#Evaluate('#qname#')#" />
</cfloop>

The Evaluate function is what allows you to do what you want.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-10 08:40

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.

查看更多
登录 后发表回答