Passes SQL statements to a data source. Not limited to queries.
<cfquery name = "query_name" dataSource = "ds_name" dbType = "type" dbServer = "dbms" dbName = "database name" connectString = "connection string" username = "username" password = "password" maxRows = "number" blockFactor = "blocksize" timeout = "milliseconds" cachedAfter = "date" cachedWithin = "timespan" provider = "COMProvider" providerDSN = "datasource" debug> SQL statements </cfquery>
cfinsert,
cfprocparam,
cfprocresult,
cfqueryparam,
cfstoredproc,
cftransaction,
cfupdate
Attribute |
Description |
---|---|
name |
Required. The name query. 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 for all dbType operations except dbType = "query" and dbType = "dynamic" . The name of the data source from which this query should retrieve data. |
dbType |
Optional. The database driver type:
|
dbServer |
Optional. For native database drivers and the SQLOLEDB provider, specifies the name of the database server computer. 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. |
connectString |
Required for dbType = "dynamic" ; optional for all others. The contents of a connection string to send to the ODBC server. When connecting to a data source defined in the ColdFusion Administrator, you can use this attribute to specify additional connection details or to override connection information specified in the Administrator. If you are dynamically connecting to a datasource by specifying dbType = "dynamic" , the connection string must specify all required ODBC connection attributes. |
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 to return in the record set. The maxRows attribute accepts an argument of -1 , which causes cfquery to return all rows in the query object. |
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. The 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. 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 is 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 . 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, you must enclose it in quotes. |
cachedWithin |
Optional. A timespan using the ColdFusion CreateTimeSpan function. Cached query data is 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 is 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 . 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 submitted to the data source and the number of records returned from the query to be returned. |
In addition to returning data from a ColdFusion data source, the cfquery
tag also returns information about the query. cfquery.ExecutionTime
returns the time it took the query to execute in milliseconds.
The cfquery
tag creates a query object, providing information in query variables as described in the following table.
You can cache query results and execute stored procedures. For information about caching cfquery
results, executing stored procedures, and displaying cfquery
output, see Developing ColdFusion Applications.
<!--- 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>