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

限于喜欢 提交于 2019-12-24 04:58:51

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12920155/is-it-possible-to-have-dynamically-generated-query-names-in-coldfusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!