Adobe COLDFUSION 9 Manual page 411

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Accessing and Using Data
SQL notes and considerations
When writing SQL in ColdFusion, keep in mind the following guidelines:
• If you use a ColdFusion variable in your SQL expression, and the variable value is a string that contains single
quotes, place the variable in a PreserveSingleQuotes function to prevent ColdFusion from interpreting the
quotation marks. The following example shows this use:
<cfset List = "'Suisun', 'San Francisco', 'San Diego'">
<cfquery name = "GetCenters" datasource = "cfdocexamples">
SELECT Name, Address1, Address2, City, Phone
FROM Centers
WHERE City IN (#PreserveSingleQuotes(List)#)
</cfquery>
• There is a lot more to SQL than what is covered here. It is a good idea to purchase one or several SQL guides for
reference.
• To perform a successful query, the data source, columns, and tables that you reference must exist.
• Some DBMS vendors use nonstandard SQL syntax (known as a dialect) in their products. ColdFusion does not
validate the SQL; it is passed on to the database for validation, so you are free to use any syntax that your database
supports. Check your DBMS documentation for nonstandard SQL usage.
Reading data from a database
You use the SQL SELECT statement to read data from a database. The SQL statement has the following general syntax:
SELECT column_names
FROM table_names
[ WHERE search_condition ]
[ GROUP BY group_expression ] [HAVING condition]
[ ORDER BY order_condition [ ASC | DESC ] ]
The statements in brackets [] are optional.
Note: There are additional options to SELECT depending on your database. For a complete syntax description for
SELECT, see the product documentation.
Results of a SELECT statement
When the database processes a SELECT statement, it returns a record set containing the requested data. The format of
a record set is a table with rows and columns. For example, if you write the following query:
SELECT * FROM employees WHERE DeptID=3
The query returns a database table. Because the data returned to ColdFusion by a SELECT statement is in the form of
a database table, ColdFusion lets you write a SQL query on the returned results. This functionality is called query of
queries. For more information on query of queries, see
The next example uses a SELECT statement to return only a specific set of columns from a table:
SELECT LastName, FirstName FROM employees WHERE DeptID=3
Filtering results
The SELECT statement lets you filter the results of a query to return only those records that meet specific criteria. For
example, if you want to access all database records for employees in department 3, you use the following query:
SELECT * FROM employees WHERE DeptID=3
"Accessing and Retrieving
Last updated 8/5/2010
Data" on page 410.
406

Advertisement

Table of Contents
loading

Table of Contents