sql-servercoldfusionqoqcoldfusion-2016

Q-of-Q Not Respecting Column Order


I am using CFSPREADSHEET to read an XLS file into a query. Then reordering the columns as necessary. However, the query-of-query I am using retains alphabetical column order.

The XLS file has the fields in the correct order, but when converted to a query the fields are alphabetized.

I output the results to a CSV using this nifty UDF and the fields are still alphabetized as proof that the q-of-q is not adhering to my request.

In order to correct this issue I'm trying to use a q-of-q to reformat the columns in their proper order.

<cfspreadsheet action="read" src="#local.tmpPath##local.tmpFile#" excludeheaderrow="true" headerrow="1" sheet="1" query="local.qTemp" />

<!--- Change column order around as necessary --->
<cfquery name="local.qTemp" dbtype="query">
    SELECT
            Name, Address, City
    FROM
            [local].qTemp
</cfquery>

<cfset local.newCSV = QueryToCSV(
    query = local.qTemp,
    fields = local.qTemp.columnlist,
    createHeaderRow = true,
    delimiter = '|'
) />

<cffile action="write" charset="windows-1252" file="#local.tmpPath#foo.csv" output="#replace(local.newCSV, """", "", "ALL")#" nameconflict="overwrite" />

The column order when I output is:

Instead of the query order:

I read this post about the topic, but it didn't address my issue specifically (unless I am reading it wrong).


Solution

  • (From comments...)

    The QoQ actually has nothing to do with the problem. The same issue would occur with the original query. Unfortunately, query.columnList ALWAYS lists columns in alphabetical order. It is a long standing gripe of many CF dev's. To get the "real" order you must use a different technique such as: