Adobe COLDFUSION 9 Manual page 436

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Accessing and Using Data
<h1>Employee List</h1>
<!--- LastNameSearch (normally generated interactively) --->
<cfset LastNameSearch="Doe">
<!--- Master Query --->
<cfquery datasource="cfdocexamples" 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>
<!--- output the detail query results --->
<p>Output using a query of query:</p>
<cfoutput query=detail>
#Emp_ID#: #FirstName# #LastName#<br>
</cfoutput>
<p>Columns in the master query:</p>
<cfoutput>
#master.columnlist#<br>
</cfoutput>
<p>Columns in the detail query:</p>
<cfoutput>
#detail.columnlist#<br>
</cfoutput>
2
Save the page as query_of_query.cfm in the myapps directory under the web_root.
Display query_of_query.cfm in your browser
3
Reviewing the code
The master query retrieves the entire Employee table from the cfdocexamples data source. The detail query selects only
the three columns to display for employees with the specified last name. The following table describes the code and its
function:
Code
cfset LastNameSearch="Doe"
<cfquery datasource="cfdocexamples" name="master"
cachedwithin=#CreateTimeSpan(0,1,0,0)#>
SELECT * from Employee
</cfquery>
<cfquery dbtype="query" name="detail">
SELECT Emp_ID, FirstName, LastName FROM master
WHERE LastName=<cfqueryparam
value="#LastNameSearch#" cfsqltype="cf_sql_char"
maxLength="20">
</cfquery>
Description
Sets the last name to use in the detail query. In a complete application,
this information comes from user interaction.
Queries the cfdocexamples data source and selects all data in the
Employees table. Caches the query data between requests to this page,
and does not query the database if the cached data is less than an hour
old.
Uses the master query as the source of the data in a new query, named
detail. This new query selects only entries that match the last name
specified by the
LastNameSearch
three columns of data: employee ID, first name, and last name. The query
uses the cfqueryparam tag to prevent passing erroneous or harmful code.
Last updated 8/5/2010
variable. The query also selects only
431

Advertisement

Table of Contents
loading

Table of Contents