<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
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
|
Specifies the column to use as primary key when returnType is set to struct. Alias: columnname, column, keycolumn |
result
string, optional
|
Specifies a name for the structure in which cfquery returns the result variables.
|
datasource
any, optional
|
The name of the data source from which this query should retrieve data. |
dbtype
string, optional
|
The following values are supported
|
tags
any, optional
|
tags stored with the cache. Alias: tag |
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
|
This is the age of which the query data can be |
cachedwithin
object, optional
|
Supported values are:
To use cached data, the current query must use the same SQL statement, params, data source, query name, user name, and password. |
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. |
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:
|
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. |
params
object, optional
|
an array or struct of params, see examples below
|
sql
string, optional
|
the SQL query to execute. |
listener
any, optional
|
|
async
boolean, optional
|
Unimplemented Attribute(s)
Examples
Tags
<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 ]
])>
<!-- bad example, not using a bound parameter, unsafe when using input from users -->
<cfquery name="q" dbtype="query">
select * from qry where name = 'jim'
</cfquery>
<cfdump var="#q#" />
<!-- using a bound parameter with cfqueryparam -->
<cfquery name="q" dbtype="query">
select * from qry where name = <cfqueryparam value='jim'>
</cfquery>
<cfdump var="#q#" />
<!-- using an array of simple params -->
<cfscript>
p = [ 'jim' ];
</cfscript>
<cfquery name="q" dbtype="query" params=#p#>
select * from qry where name = ?
</cfquery>
<cfdump var="#q#" />
<!-- using an array of struct params -->
<cfscript>
p = [ { value='jim', sqltype='varchar' } ];
</cfscript>
<cfquery name="q" dbtype="query" params=#p#>
select * from qry where name = ?
</cfquery>
<cfdump var="#q#" />
<!-- using an array of named struct params -->
<cfscript>
p = [id= { value='jim', sqltype='varchar' } ];
</cfscript>
<cfquery name="q" dbtype="query" params=#p#>
select * from qry where name = :id
</cfquery>
<cfdump var="#q#" />
Script
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 ]
])
<span class="nv">query</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"q"</span> <span class="nv">dbtype</span><span class="o">=</span><span class="s2">"query"</span> <span class="p">{</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">"select * from qry where name = 'jim'"</span><span class="p">);</span>
<span class="p">}</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nv">q</span><span class="p">);</span>
<span class="nv">query</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"q"</span> <span class="nv">dbtype</span><span class="o">=</span><span class="s2">"query"</span> <span class="p">{</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">"select * from qry where name = "</span><span class="p">)</span>
<span class="nv">queryParam</span> <span class="nv">cfsqltype</span><span class="o">=</span><span class="s2">"cf_sql_varchar"</span> <span class="nv">value</span><span class="o">=</span><span class="s2">"jim"</span><span class="p">;</span>
<span class="p">}</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nv">q</span><span class="p">);</span>
<span class="nv">p</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">'jim'</span> <span class="p">];</span>
<span class="nv">query</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"q"</span> <span class="nv">dbtype</span><span class="o">=</span><span class="s2">"query"</span> <span class="nv">params</span><span class="o">=</span><span class="err">#</span><span class="nv">p</span><span class="err">#</span><span class="p">{</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">"select * from qry where name = ?"</span><span class="p">);</span>
<span class="p">}</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nv">q</span><span class="p">);</span>
<span class="nv">p</span> <span class="o">=</span> <span class="p">[</span><span class="nv">id</span><span class="o">=</span> <span class="p">{</span> <span class="nv">value</span><span class="o">=</span><span class="s1">'jim'</span><span class="p">,</span> <span class="nv">sqltype</span><span class="o">=</span><span class="s1">'varchar'</span> <span class="p">}</span> <span class="p">];</span>
<span class="nv">query</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"q"</span> <span class="nv">dbtype</span><span class="o">=</span><span class="s2">"query"</span> <span class="nv">params</span><span class="o">=</span><span class="err">#</span><span class="nv">p</span><span class="err">#</span><span class="p">{</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">"select * from qry where name = :id"</span><span class="p">);</span>
<span class="p">}</span>
<span class="nf">writedump</span><span class="p">(</span><span class="nv">q</span><span class="p">);</span>
See also
- Cache
- Core CFML Language
- Queries
- Cache a query for the current request
- Query Listeners
- IsWithinTransaction()
- Query()
- QueryExecute()
- Query of Queries sometimes it rocks, sometimes it sucks
- Query Handling In Lucee
- Query of Queries (QoQ)
- <cfqueryparam>
- <cfstoredproc>
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)