Building Queries

As discussed earlier in this chapter, you build queries using the CFQUERY tag and SQL.

Note To query the table:
  1. Create a new application page.
  2. Edit the page so that it appears as follows:
    <HTML>
    <HEAD>
    <TITLE>Employee List</TITLE>
    </HEAD>
    <BODY>
    <H1>Employee List</H1>
    <CFQUERY NAME="EmpList" DATASOURCE="CompanyInfo">
        SELECT FirstName, LastName, Salary, Contract
        FROM Employees
    </CFQUERY>
    </BODY>
    </HTML>
    
  3. Save the page as emplist.cfm in myapps under the Web root directory. For example, the directory path on your machine may be:

    C:\INETPUB\WWWROOT\myapps on Windows NT

  4. Return to your browser and enter the following URL to view EmpList.cfm:
    http://127.0.0.1/myapps/emplist.cfm
    
  5. View source in the browser.

    The ColdFusion EmpList data set is created by ColdFusion Server, but only HTML and text is sent back to the browser. To display the data set on the page, you must code tags and variables to output the data.

Code Review

The query you just created retrieves data from the CompanyInfo database.

Code Description
<CFQUERY NAME="EmpList" 
DATASOURCE="CompanyInfo">
Query the database specified in the CompanyInfo datasource
SELECT FirstName, LastName, 
Salary, Contract
FROM Employees
Get information from the FirstName, LastName, Salary, and Contract fields in the Employees table
</CFQUERY>
End the CFQUERY block

Query Notes and Considerations

When creating queries to retrieve data, keep these guidelines in mind: