Querying a Query

In Coldfusion programming, queries are run within the <cfquery tag and the information is stored within a structure called after the argument within the name attribute. The information can then be accessed within the structure where applied, since not all queries return information. You can then search the records in the structure by running another query on the query. What needs to change in the second query is the attribute datasource =”xxxxx” needs to get removed within the tag then replaced with dbtype=”query”. Also, where the table name would be listed with the second query, it would be replaced with the name of the first query. Some reasons for querying a query would be to manipulate data within the structure then running select statements on it or to run multiple queries on selected data.

<cfquery name=“queryname1″ datasource=“dsn”>
select *
from dbtable
where dbfield1 = ‘XXXX’
</cfquery>

<CFQUERY NAME=“queryname2″ dbtype=“query”>
select dbfield2
from queryname1
where dbfield1 = ‘XXXX’
</CFQUERY>