
Retrieving data
429
The cfquery tag
The
cfquery
tag is one of the most frequently used CFML tags. You use it with the
cfoutput
tag
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="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
<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
later on the page.
•
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 451
.
•
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.
Содержание ColdFusion MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...