# CFQUERY

CFQUERY passes SQL statements for any purpose to your data source. Not limited to queries.

### Syntax

    <CFQUERY NAME="query_name"
        DATASOURCE="ds_name"
        DBTYPE="type"
        DBSERVER="dbms"
        DBNAME="database name"
        USERNAME="username"
        PASSWORD="password"
        MAXROWS="number"
        BLOCKFACTOR="blocksize"
        TIMEOUT="milliseconds"
        CACHEDAFTER="date"
        CACHEDWITHIN="timespan"
        PROVIDER="COMProvider"
        PROVIDERDSN="datasource"
        DEBUG="Yes/No">

    SQL statements

    </CFQUERY>

#### NAME

Required. The name you assign to the query. Query names must begin with a letter and may consist of letters, numbers, and the underscore character (spaces are not allowed). The query name is used later in the page to reference the query's record set.

#### DATASOURCE

Required. The name of the data source from which this query should retrieve data.

#### DBTYPE

Optional. The database driver type:

- ODBC (default) -- ODBC driver.
- Oracle73 -- Oracle 7.3 native database driver. Using this option, the ColdFusion Server computer must have Oracle 7.3.4.0.0 (or greater) client software installed.
- Oracle80 --Oracle 8.0 native database driver. Using this option, the ColdFusion Server computer must have Oracle 8.0 (or greater) client software installed.
- Sybase11 --Sybase System 11 native database driver. Using this option, the ColdFusion Server computer must have Sybase 11.1.1 (or greater) client software installed. Sybase patch ebf 7729 is recommended.
- OLEDB --OLE DB provider. If specified, this database provider overrides the driver type specified in the ColdFusion Administrator.
- DB2 --DB2 5.2 native database driver.
- Informix73 --Informix73 native database driver.

#### DBSERVER

Optional. For native database drivers and the SQLOLEDB provider, specifies the name of the database server machine. If specified, DBSERVER overrides the server specified in the data source.

#### DBNAME

Optional. The database name (Sybase System 11 driver and SQLOLEDB provider only). If specified, DBNAME overrides the default database specified in the data source.

#### USERNAME

Optional. If specified, USERNAME overrides the username value specified in the data source setup.

#### PASSWORD

Optional. If specified, PASSWORD overrides the password value specified in the data source setup.

#### MAXROWS

Optional. Specifies the maximum number of rows you want returned in the record set.

#### BLOCKFACTOR

Optional. Specifies the maximum number of rows to fetch at a time from the server. The range is 1 (default) to 100. This parameter applies to ORACLE native database drivers and to ODBC drivers. Certain ODBC drivers may dynamically reduce the block factor at runtime.

#### TIMEOUT

Optional. Lets you specify a maximum number of milliseconds for the query to execute before returning an error indicating that the query has timed-out. This attribute is not supported by most ODBC drivers. TIMEOUT is supported by the SQL Server 6.x or above driver. The minimum and maximum allowable values vary, depending on the driver.

#### CACHEDAFTER

Optional. Specify a date value (for example, 4/16/98, April 16, 1999, 4-16-99). ColdFusion uses cached query data if the date of the original query is after the date specified. Effective only if query caching has been enabled in the ColdFusion Administrator. To use cached data, the current query must use the same SQL statement, data source, query name, user name, password, and DBTYPE. Additionally, for native drivers it must have the same DBSERVER and DBNAME (Sybase only).

Years from 0 to 29 are interpreted as 21st century values. Years 30 to 99 are interpreted as 20th century values.

When specifying a date value as a string, make sure it is enclosed in quotes.

#### CACHEDWITHIN

Optional. Enter a timespan using the ColdFusion CreateTimeSpan function. Cached query data will be used if the original query date falls within the time span you define. The CreateTimeSpan function is used to define a period of time from the present backwards. Effective only if query caching has been enabled in the ColdFusion Administrator. To use cached data, the current query must use the same SQL statement, data source, query name, user name, password, and DBTYPE. Additionally, for native drivers it must have the same DBSERVER and DBNAME (Sybase only).

#### PROVIDER

Optional. COM provider (OLE-DB only).

#### PROVIDERDSN

Optional. Data source name for the COM provider (OLE-DB only).

#### DEBUG

Optional. Used for debugging queries. Specifying this attribute causes the SQL statement actually submitted to the data source and the number of records returned from the query to be output.

### Usage

In addition to returning data from a ColdFusion data source, the CFQUERY tag also returns informations about the query. CFQUERY.ExecutionTime returns the time it took the query to execute in milliseconds.

CFQUERY creates a query object, providing you information in three query variables as described in the following table.

CFQUERY Variables
Variable Name | Description
--- | ---
query_name.RecordCount | The total number of records returned by the query.
query_name.CurrentRow | The current row of the query being processed by CFOUTPUT.
query_name.ColumnList | Returns a comma-delimited list of the query columns.

You can cache query results and execute stored procedures. For information about caching CFQUERY results, executing stored procedures, and displaying CFQUERY output, see Developing Web Applications with ColdFusion.

### Example

    <!--- This example shows the use of CFQUERY --->

    <HTML>
    <HEAD>
        <TITLE>CFQUERY Example</TITLE>
    </HEAD>

    <BODY>
    <H3>CFQUERY Example</H3>

    <!--- define startrow and maxrows to facilitate
        'next N' style browsing --->
    <CFPARAM NAME="MaxRows" DEFAULT="10">
    <CFPARAM NAME="StartRow" DEFAULT="1">

    <!--- query database for information --->
    <CFQUERY NAME="GetParks" DATASOURCE="cfsnippets">
    SELECT      PARKNAME, REGION, STATE
    FROM         Parks
    ORDER by ParkName, State
    </CFQUERY>

    <!--- build HTML table to display query --->
    <TABLE cellpadding=1 cellspacing=1>
    <TR>
        <TD colspan=2 bgcolor=f0f0f0>
        <B><I>Park Name</I></B>
        </TD>
        <TD bgcolor=f0f0f0>
        <B><I>Region</I></B>
        </TD>
        <TD bgcolor=f0f0f0>
        <B><I>State</I></B>
        </TD>
    </TR>

    <!--- Output the query and define the startrow and maxrows
          parameters. Use the query variable CurrentCount to
          keep track of the row you are displaying. --->
    <CFOUTPUT QUERY="GetParks" StartRow="#StartRow#" MAXROWS="#MaxRows#">
    <TR>
        <TD valign=top bgcolor=ffffed>
        <B>#GetParks.CurrentRow#</B>
        </TD>
        <TD valign=top>
        <FONT SIZE="-1">#ParkName#</FONT>
        </TD>
        <TD valign=top>
        <FONT SIZE="-1">#Region#</FONT>
        </TD>
        <TD valign=top>
        <FONT SIZE="-1">#State#</FONT>
        </TD>
    </TR>
    </CFOUTPUT>

    <!--- If the total number of records is less than or equal
    to the total number of rows, then offer a link to
    the same page, with the StartRow value incremented by
    MaxRows (in the case of this example, incremented by 10) --->
    <TR>
        <TD colspan=4>
        <CFIF (StartRow + MaxRows) LTE GetParks.RecordCount>
            <a href="cfquery.cfm?startrow=<CFOUTPUT>#Evaluate(StartRow +
            MaxRows)#</CFOUTPUT>">See next <CFOUTPUT>#MaxRows#</CFOUTPUT>
            rows</A>
        </CFIF>

        </TD>
    </TR>
    </TABLE>
    </BODY>
    </HTML>