User Guide
Chapter 3: Querying a Database 25
Query Notes and Considerations
When creating queries to retrieve data, keep these guidelines in mind:
• Enter the query NAME and DATASOURCE attributes in the begin CFQUERY tag.
• Surround attribute settings with double quotes(").
• Reference the query data by naming the query in the CFOUTPUT tag later on
the page.
• Make sure that a data source exists in the ColdFusion Administrator before you
reference iit n a CFQUERY tag.
• The SQL that you write is sent to the database and performs the actual data
retrieval.
• Columns and tables that you refer to in your SQL statement must exist,
otherwise the query will fail.
Outputting Query Data
After you have defined a query on a page, you can use the CFOUTPUT tag with the
QUERY attribute to define the query variable that you want to output to a page. When
you use the QUERY attribute:
• ColdFusion loops over all the code contained within the CFOUTPUT block,
once for each row returned from a database.
• Reference specific column names within the CFOUTPUT block to output the
data to the page.
• You can place text and HTML tags inside or surrounding the CFOUTPUT block
to format the data on the page.
The CFOUTPUT tag accepts a variety of optional attributes but, ordinarily, you will use
the QUERY attribute to define the name of an existing query.
To output query data on your page:
1. Return to empList.cfm in Studio.
2. Edit the file so that it appears as follows:
<HTML>
<HEAD>
<TITLE>Employee List</TITLE>
</HEAD>
<BODY>
<H1>Employee List</H1>
<CFQUERY NAME="EmpList" DATASOURCE="CompanyInfo">
SELECT FirstName, LastName, Salary, Contract
FROM Employees
</CFQUERY>
4 <CFOUTPUT QUERY="EmpList">