Search code examples
coldfusioncoldfusion-9qoq

Merging a array of queries into a single query


I want to merge an array of queries into one query

I have tried this

<cfquery name="MergedData" dbtype="query">
    <cfloop from="1" to="#arrayLen(arData)#" index="k"> 
        SELECT *
        FROM    arData[k]
        <cfif k LT ArrayLen(arData)>UNION</cfif>
    </cfloop>
    ORDER BY EID
</cfquery>

I get an error that looks like

<br><b>Query Of Queries syntax error.</b><br> Encountered "[k]. 

Solution

  • This is what end up working

    <cfquery name="local.qryMergedData" dbtype="query">
        <cfloop array="#arguments.Data#" index="k">
            <cfset local.currentIndex++>
            <cfset setvariable("Data_#local.currentIndex#", k)>
    
            SELECT *
            FROM   Data_#local.currentIndex#
            <cfif local.currentIndex LT local.MaxIndex>UNION</cfif>
        </cfloop>
        ORDER BY EID
    </cfquery>  
    

    I really don't like that I am setting a variable inside of a <cfquery>, but at least it is only one query