User Guide
26 Chapter 3 Querying a Database
• When referencing text literals in SQL, use single quotes (’). For example, Select
* from mytable WHERE FirstName=’Russ’
selects every record from mytable in
which the first name is Russ.
Writing SQL
In between the begin and end cfquery tags, write the SQL that you want the
database to execute.
For example, to retrieve data from a database:
• Write a SELECT statement that lists the fields or columns that you want to select
for the query.
• Follow the SELECT statement with a FROM clause that specifies the database
tables that contain the columns.
Tip
If you are using ColdFusion Studio, you can use the Query Builder to build SQL
statements by graphically selecting the tables and records within those tables that
you want to retrieve.
When the database processes the SQL, it creates a data set (a structure containing
the requested data) that is returned to ColdFusion Server. ColdFusion places the
data set in memory and assigns it the name that you defined for the query in the
name
attribute of the
cfquery tag.
You can reference that data set by name using the
cfoutput tag later on the page.
Basic SQL syntax elements
The following sections present brief descriptions of the main SQL command
elements.
Statements
A SQL statement always begins with a SQL verb. The following keywords identify
commonly used SQL verbs:
Keyword Description
SELECT Retrieves the specified records
INSERT Adds a new row
UPDATEw Changes values in the specified rows
DELETE Removes the specified rows