<cfquery>

Passes SQL statements to a data source. Not limited to queries.

This tag may have a body.

This tag is also supported within <cfscript>

<cfquery name=string columnkey=string result=string datasource=any dbtype=string dbserver=string dbname=string tags=any connectstring=string username=string password=string maxrows=number blockfactor=number timeout=any cachedafter=datetime cachedwithin=object provider=string providerdsn=string debug=boolean cachename=string psq=boolean unique=boolean ormoptions=struct indexname=string returntype=string timezone=timezone lazy=boolean params=object nestinglevel=numeric sql=string listener=any async=boolean cacheid=string ><!--- body --->[</cfquery>]
Attribute Description
name
string, optional

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.

Alias: variable

columnkey
string, optional

When return type is set to "struct", the value of this column is used as key in the struct for every record.

Alias: columnname, column, keycolumn

* hidden *
result
string, optional

Specifies a name for the structure in which cfquery returns the result variables.

  • SQL: The SQL statement that was executed. (string)
  • Cached: If the query was cached. (boolean)
  • SqlParameters: An ordered Array of cfqueryparam values. (array)
  • RecordCount: Total number of records in the query. (numeric)
  • ColumnList: Column list, comma separated. (numeric)
  • ExecutionTime: Execution time for the SQL request. (numeric)
datasource
any, optional

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

dbtype
string, optional

support the following values: - query: for doing a query on an existing query object - hql: for doing a query on orm

dbserver
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
dbname
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
tags
any, optional

tags stored with the cache.

Alias: tag

* hidden *
connectstring
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
username
string, optional

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

password
string, optional

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

maxrows
number, optional

Specifies the maximum number of rows to return in the record set.

blockfactor
number, 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
any, optional

The maximum number of seconds 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
datetime, optional

Sets a date/time from when the tag will start to cache the query. In difference to other CFML Engines this attribute only works in combination with the tag "cachedWithin", so you still have to set with "cachedWithin" how long the query remains in the cache. To match other CFML engines simply set "cachedWithin" to "36500", that will hold it in cache for 100 years!

cachedwithin
object, optional

possible values are: String "request": If original query was created within the current request, cached query data isused. a timespan (created with function CreateTimeSpan): If original query date falls within the time span, cached query data isused.

To use cached data, the current query must use the same SQL statement, data source, query name, user name, and password.

provider
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
providerdsn
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
debug
boolean, 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.

cachename
string, optional

This attribute has been deprecated and is non-functional.

* deprecated *
psq
boolean, optional

preserve single quote or not

unique
boolean, optional

Specifies if the object parameter is unique, used only for dbtype=orm or hql

ormoptions
struct, optional

Object parameter for the entity.

indexname
string, optional
returntype
string, optional

one of the following values:

  • query: default for all dbtype expect "hql", returns a query object
  • array_of_entity: works only with dbtype "hql" and is also the default value for dbtype "hql"
  • array: converts the query object into an array of structs
  • struct: converts the query object into a struct using the columnKey attribute as a primary key
timezone
timezone, optional

the timezone used to convert a date object to a timestamp (string), this value is needed when your database runs in another timezone and you are not using cfqueryparam to insert dates.

lazy
boolean, optional

If "lazy" is set to true (default "false") Lucee does not initially load all the data from the datasource.

When "true" the data is only loaded when requested, this means the data is dependant on the datasource connection. If the datasource connection has been lost for some reason and the data has not yet been requested, Lucee throws an error if you try to access the data.

The "lazy" attribute only works if the following attributes are not used: cachewithin, cacheafter and result.

params
object, optional
nestinglevel
numeric, optional

the level for the current pagesource

* hidden *
sql
string, optional

the SQL query to execute.

listener
any, optional
* hidden *
async
boolean, optional
* hidden *
cacheid
string, optional

Attribute not supported

* unimplemented *

Examples

Simple Example

<cfset qry= queryNew("name,age,whatever", "varchar,date,int", [
[ "Susi", CreateDate( 1970, 1, 1 ), 5 ],
[ "Urs" , CreateDate( 1995, 1, 1 ), 7 ],
[ "Fred", CreateDate( 1960, 1, 1 ), 9 ],
[ "Jim" , CreateDate( 1988, 1, 1 ), 11 ]
])>
<!-- QofQ -->
<cfquery name="selectName" dbtype="query">
	select * from qry where name='jim'
</cfquery>
<cfdump var="#selectName#" />

See also