User Guide

Table Of Contents
Retrieving data 467
In this example, the query code tells ColdFusion to do the following:
Connect to the cfdocexamples data source (the cfdocexamples.mdb database).
Execute SQL code that you specify.
Store the retrieved data in the query object EmpList.
When creating queries to retrieve data, keep the following guidelines in mind:
You must use opening <cfquery> and ending </cfquery> tags, because the cfquery tag is a
block tag.
Enter the query name and datasource attributes within the opening cfquery tag.
To tell the database what to process during the query, place SQL statements inside the cfquery
block.
When referencing text literals in SQL, use single-quotation marks ('). For example, SELECT *
FROM mytable WHERE FirstName='Jacob'
selects every record from mytable in which the
first name is Jacob.
Surround attribute values with double quotation marks (“attrib_value”).
Make sure that a data source exists in the ColdFusion MX Administrator before you reference
it in a
cfquery tag.
Columns and tables that you refer to in your SQL statement must exist, otherwise the query
will fail.
Reference the query data by naming the query in one of the presentation tags, such as
cfoutput, cfgrid, cftable, cfgraph, or cftree.
When ColdFusion returns database columns, it removes table and owner prefixes. For
example, if you query Employee.Emp_ID in the query, the Employee, is removed and returns
as Emp_ID. You can use an alias to handle duplicate column names; for more information, see
Chapter 22, “Using Query of Queries,” on page 489.
You cannot use SQL reserved words, such as MIN, MAX, COUNT, in a SQL statement.
Because reserved words are database-dependent, see your databases documentation for a list of
reserved words.
Building queries
As discussed earlier in this chapter, you build queries using the
cfquery tag and SQL.
Note: This and many subsequent procedures use the cfdocexamples data source that connects to
the cfdocexamples.mdb database. This data source is installed by default. For information on adding
or configuring a data source, see Configuring and Administering ColdFusion MX.