User Guide

Table Of Contents
466 Chapter 20: Accessing and Retrieving Data
To query a database, you must use:
ColdFusion data sources
The cfquery tag
SQL commands
Retrieving data
You can query databases to retrieve data at runtime. The retrieved data, called the record set, is
stored on that page as a query object. A query object is a special entity that contains the record set
values, plus RecordCount, CurrentRow, ColumnList, SQL, Cached, and SQLParameter query
variables. You specify the query object’s name in the
name attribute of the cfquery tag. The query
object is often called simply the query.
The following is a simple
cfquery tag:
<cfquery name = "GetSals" datasource = "cfdocexamples">
SELECT * FROM Employee
ORDER BY LastName
</cfquery>
Note: The terms “record set” and “query object” are often used synonymously when discussing
record sets for queries. For more information, see Chapter 22, “Using Query of Queries,” on
page 489.
When retrieving data from a database, perform the following tasks:
To tell ColdFusion how to connect to a database, use the cfquery tag on a page.
To specify the data that you want to retrieve from the database, write SQL commands inside
the
cfquery block.
Reference the query object and use its data values in any tag that presents data, such as
cfoutput, cfgrid, cftable, cfgraph, or cftree.
The cfquery tag
The
cfquery tag is one of the most frequently used CFML tags. You use it to retrieve and
reference the data returned from a query. When ColdFusion encounters a
cfquery tag on a page,
it does the following:
Connects to the specified data source.
Performs SQL commands that are enclosed within the block.
Returns result set values to the page in a query object.
The cfquery tag syntax
The following code shows the syntax for the
cfquery tag:
<cfquery name="EmpList" datasource="cfdocexamples">
SQL code...
</cfquery>