About Query of Queries
455
•
You can obtain drill-down, master-detail information for which you do not access the database
for the details.
For example, you can select information about departments and employees in a query, and
cache the results. You can then display the employees’ names. When users select an employee,
the application displays the employee’s details by selecting information from the cached query,
without accessing the database.
Performing a Query of Queries
There are four steps to perform a Query of Queries.
To perform a Query of Queries:
1
Generate a record set.
You can write a normal query using a tag or function that creates a record set. This is
sometimes called a
master query
. For more information, see
“Creating a record set”
on page 452
.
2
Write a
detail query
—a query that specifies
dbtype=
"
query"
in its
cfquery
tag.
3
In the detail query, write a SQL statement that retrieves the relevant records. Specify the names
of one or more existing queries as the table names in your SQL code. Do not specify a
datasource
attribute.
4
If the database content does not change rapidly, use the
cachedwithin
attribute of the master
query to cache the query results between page requests. This way, ColdFusion accesses the
database on the first page request, and does not query the database again until the specified time
expires. You must use the
CreateTimeSpan
function to specify the
cachedwithin
attribute
value (in days, hours, minutes, seconds format).
The detail query generates a new query results set, identified by the value of the
name
attribute of
the detail query. The following example illustrates the use of a master query and a single detail
query that extracts information from the master.
To use the results of a query in a query:
1
Create a ColdFusion page with the following content:
<body>
<h1>Employee List</h1>
<!--- LastNameSearch (normally generated interactively) --->
<cfset LastNameSearch="Doe">
<!--- Master Query --->
<cfquery datasource="CompanyInfo" name="master"
cachedwithin=#CreateTimeSpan(0,1,0,0)#>
SELECT * from Employee
</cfquery>
<!--- Detail Query (dbtype=query, no data source) --->
<cfquery dbtype="query" name="detail">
SELECT Emp_ID, FirstName, LastName
FROM master
WHERE LastName=<cfqueryparam value="#LastNameSearch#"
cfsqltype="cf_sql_char" maxLength="20"></cfquery>
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......