ShanghaiD
asked on
Query of Queries in cfcomponent
I've successfully executed a Query of Queries on a cfml page where the code for each cfquery was in the header of that page. Now I'm trying to move the two queries to a cfc to be accessed remotely.
My cfc has the following code (getOffices is a simplified dummy query for illustrative purposes -- the actual query queries several mySQL tables):
My cfc has the following code (getOffices is a simplified dummy query for illustrative purposes -- the actual query queries several mySQL tables):
<cffunction name="getOffices" access="remote" output="no" returntype="query">
<cfquery name="q">
SELECT "Brisbane" as city, "QLD" as state
UNION
SELECT "Melbourne" as city, "Vic" as state
UNION
SELECT "Sydney" as city, "NSW" as state
UNION
SELECT "Cairns" as city, "QLD" as state
UNION
SELECT "Geelong" as city, "Vic" as state
UNION
SELECT "Newcastle" as city, "NSW" as state
</cfquery>
<cfreturn q>
</cffunction>
<cffunction name="getStates" access="remote" output="no" returntype="query">
<cfset theQuery = getOffices()>
<cfquery dbtype="query" name="q">
SELECT DISTINCT state
FROM #theQuery#
ORDER BY state
</cfquery>
<cfreturn q>
</cffunction>
When I try to invoke the getStates method from my cfml page, I get an error message:Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.How do I get things to work?
The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SidFishes: Thank you! I "thought" I had tried that (without the # #) before I posted my question (as well as other alternatives for the syntax) but clearly I had not.
_agx_: Thanks for the clear explanation of what was going wrong. Also, I will follow your recommendation to var scope in future.
_agx_: Thanks for the clear explanation of what was going wrong. Also, I will follow your recommendation to var scope in future.
Welcome :)
The reason #theQuery# doesn't work is because the QoQ table name must be a simple string. By using # signs, CF evaluates #theQuery# first and passes the query object to the QoQ, instead of the name.
Also, to avoid threading problems you should var scope both the "thequery" and "q" variables - in both functions.