The cfquery tag
The
tag is one of the most frequently used CFML tags. You use it with the
cfquery
to retrieve and reference the data returned from a query. When ColdFusion encounters a
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 name="EmpList" datasource="CompanyInfo">
SQL code...
</cfquery>
In this example, the query code tells ColdFusion to do the following:
•
Connect to the CompanyInfo data source (the company.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
block tag.
•
Enter the query
•
To tell the database what to process during the query, place SQL statements inside the
block.
•
When referencing text literals in SQL, use single quotation marks ('). For example,
FROM mytable WHERE FirstName='Jacob'
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
•
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
•
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
•
You cannot use SQL reserved words, such as MIN, MAX, COUNT, in a SQL statement.
Because reserved words are database-dependent, see your database's documentation for a list of
reserved words.
and ending
<cfquery>
and
name
datasource
tag.
,
,
cftable
cfgraph
tag:
cfquery
tags, because the
</cfquery>
attributes within the opening
selects every record from mytable in which the
, or
later on the page.
cftree
451.
tag
cfoutput
cfquery
tag is a
cfquery
tag.
cfquery
cfquery
SELECT *
Retrieving data
429
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers