Posted in ColdFusion | Posted on 08-28-2009 | 3,372 views
About two months ago I blogged about an interesting Query of Query bug. The bug involved a QoQ that changed the sort order of the original data. This week a user sent in another example of a QoQ causing changes to the original query. Consider the following query:
2
3<cfloop index="loop1" from="1" to="5">
4 <cfset newrow = queryaddrow(mydata, 1)>
5 <cfset temp = querysetcell(mydata, "mydate", #dateformat(now()-loop1,"dd-mmm-yy")#, #loop1#)>
6 <cfset temp = querysetcell(mydata, "rannum", 55.65, #loop1#)>
7</cfloop>
The query contains two columns, mydate and rannum. (I just now noticed the space in his list of columns. ColdFusion appears to auto-trim it. I'd remove that from the code however.) Dumping the query shows:

Now consider this simple QofQ:
2select mydate, rannum from mydata
3</cfquery>
After running this, a new dump of mydata shows:

The dates in the original query have now been converted into full date/time stamps. This comes from the fact that ColdFusion noticed they were dates and treated them as such. I think that makes sense, but it still bugs me that it changes the original query.
You can kind of fix it by forcing a type on the queryNew:
Of course, that's kinda sucky as well. I bet if you sort on it you will get inconsistent results. The best fix then would be to duplicate the query before you run the QofQ on it. That way you can go ahead and let ColdFusion convert the column to dates and still get proper sorting.
Oh - and in case you were wondering - the formats of the date in the query were pretty important. He was feeding the query to a chart and needed it to show up correctly.


As for the chart formatting, I haven't used cfchart in a long time. Seems like the issue is how to get a date field from a database into cfchart in a specific format.
<cfset myQuery = QueryNew("agent,name") />
<cfset QueryAddRow(myQuery, 1) />
<cfset QuerySetCell(myQuery, "agent", "007") />
<cfset QuerySetCell(myQuery, "name", "James Bond") />
<cfset QueryAddRow(myQuery, 1) />
<cfset QuerySetCell(myQuery, "agent", "86") />
<cfset QuerySetCell(myQuery, "name", "Maxwell Smart") />
<cfdump var="#myQuery#">
<cfquery dbtype="query" name="queryAgents">
select agent, name from myQuery
</cfquery>
<cfdump var="#queryAgents#">
I guess I would prefer that QofQ translates that to a date object so it could keep the sorting I would expect (year-month-day, not alphabetical). If the QofQ did not do that translation, as you mentioned, a potential Order By would get wacky.
Oh, and no sweat over the yelling. Nothing like a good Friday rumble distraction. :)
Yes, defining the column types fixes the problem.
That feature was introduced several CF versions ago (I forget offhand which). Before that it was a real problem, now it's just something to look out for.
I finally see what you're getting at. Didn't read so closely. When I went back and cfdumped my original 'myQuery', it, too, had changed, not just the QoQ version. I agree that this isn't good.
[Add Comment] [Subscribe to Comments]