I have come across a very interesting error when doing a query of a query inside a CFSCRIPT tag...
I get a result set back from a SQL Server stored procedure. It is a list of metric names (METRIC_ID) and the actual metric measurements.
I need to pull the METRIC_ID from this result set to do a JOIN on another query that has display and other formatting attributes for display in a web page.
The following code executes with no errors when the query is unordered:
var displayDataQuery = new Query();
displayDataQuery.setName("joinedQuery");
displayDataQuery.setDBType("query");
displayDataQuery.setAttributes(qry1 = fullMetricsListQuery);
displayDataQuery.setAttributes(qry2 = arguments.cubeData);
displayDataQuery.setSQL("SELECT METRIC_ID FROM qry2");
var tempResultSet = displayDataQuery.execute();
var cubeMetricIDList = tempResultSet.getResult();
When I take the same exact query results from the same stored procedure, but it's ordered by an ORDER BY clause, this statement;
var tempResultSet = displayDataQuery.execute();
throws the following error - "The value '' cannot be converted to a number." That's 2 single quotes, signifying an empty string between "value" and "cannot."
Wait, what??? I just want a list of metric names, varchars. Why is there a conversion error...?
The error ultimately points back to ColdFusion10\cfusion\CustomTags\com\adobe\coldfusion\base.cfc, line 445.
OK, this is where this takes an interesting turn...
Starting at line 444 in base.cfc, Line 445 bolded in red:
<cfif sqlType neq "" and arraylen(sqlParams) gt 0>
<cfloop index="i" from="2" to="#ArrayLen(sqlArray)#">
<cfif (i-1) lte arraylen(sqlParams)>
<cfqueryparam attributeCollection="#sqlParams[i-1]#"/>
</cfif>
#getPreserveSingleQuotes(sqlArray[i])#
</cfloop>
</cfif>
So if I understand this correctly, when I have an unordered query, this code in base.cfc reads the SQL set in displayDataQuery.setSQL("SELECT METRIC_ID FROM qry2"), but doesn't when the query is ordered by METRIC_ID...?
Ah, wait... the CFDUMP when using an ordered data set says something else...
query | |
---|---|
RESULTSET | |
CACHED | false |
EXECUTIONTIME | 4 |
SQL | SELECT * FROM qry1 ORDER BY METRIC_ID, LOCATION |
I'm pretty much stumped why the same data in unordered set works while in an ordered set causes this error.
Any and all helpful suggestions or clues will be greatly appreciated!!!