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:

When the database processes the SQL, it creates a data set 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 begin CFQUERY tag.

You may reference that data set by name using the CFOUTPUT tag further down on the page.

Note Note:There is a lot more to SQL than what is covered here. Allaire recommends that you purchase one or several SQL guides that you can refer to as you go.

SQL usage example

For example, you would write the code below to retrieve the FirstName, LastName, StartDate, Salary, and Contract columns from the Employees table located in the database C:\CFUSION\DATABASE\HRExpress.mdb:

<CFQUERY NAME="EmpList" DATASOURCE="HRApp">
    SELECT FirstName, LastName, StartDate, Salary, Contract
    FROM Employees
</CFQUERY>
Note Note:The data source, columns, and tables that you reference must exist in order to perform a successful query. For this reason, all of the examples in this guide refer to the HRapp data source; a data source that is delivered with the product.