Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use query-of-query UNION on n-recordsets when var scoping is needed?

Tags:

I would like to be able to do a query of a query to UNION an unknown number of recordset. However when doing a query-of-query dots or brackets are not allowed in record set names.

For example this fails:

<cfquery name="allRecs" dbtype="query">
    SELECT * FROM recordset[1]
    UNION
    SELECT * FROM recordset[2]
</cfquery>

Using dynamic variable names such as "recordset1" work but this is in a function and needs to be var-scoped so I can't build up the variable names dynamically without producing memory leaks in a persisted object.

Any other ideas?

like image 573
Dan Roberts Avatar asked Jan 27 '09 20:01

Dan Roberts


People also ask

What is query of queries in ColdFusion?

Last updated on 17 Jan 2022 | Also Applies to ColdFusion More. After you have created a recordset with a tag or function, you can retrieve data from the recordset in one or more dependent queries. A query that retrieves data from a recordset is called a Query of Queries.

What is a union query?

The Union operator combines the results of two or more queries into a distinct single result set that includes all the rows that belong to all queries in the Union. In this operation, it combines two more queries and removes the duplicates.

Which of the following statements is correct on a union SQL query?

Answer: B) Combines the output from multiple queries and must include the same number of columns. Explanation: A UNION query combines the output from multiple queries and must include the same number of columns. Grouping items with similar properties together.


1 Answers

After posting the question I came up with a couple solutions but there might be a better one out there

  • I could write dynamically named variables to the arguments scope and then reference them without their scope in query

  • Create a function that accepts 2 recordsets as arguments and returns one combined recordset. This could be looped over to progressively add a recordset at a time. I'm sure this is very inefficient compared to doing all UNIONs in one query though.

like image 62
Dan Roberts Avatar answered Sep 18 '22 19:09

Dan Roberts