cfqueryparam
Verifies the data type of a query parameter and, for DBMSs that support bind variables, enables CFML to use bind variables in the SQL statement. Bind variable usage enhances performance when executing a cfquery statement multiple times. This tag is nested within a cfquery tag, embedded in a query SQL statement. If you specify optional parameters, this tag performs data validation.NOTE: Due to security it's highly recommended to use this tag for any user input or non-static value used in a query to prevent code injections and the like.
<cfqueryparam>
cfqueryparam();
Attribute Reference
value string
cfsqltype string
Default: CF_SQL_CHAR
CF_SQL_CHAR
cf_sql_ prefix. See [CFSqlType Cheatsheet](https://cfdocs.org/cfsqltype-cheatsheet) for a mapping of CFSQL data types to DBMS data types. cf_sql_bigintcf_sql_bitcf_sql_charcf_sql_blobcf_sql_clobcf_sql_nclobcf_sql_datecf_sql_decimalcf_sql_doublecf_sql_floatcf_sql_idstampcf_sql_integercf_sql_longvarcharcf_sql_longnvarcharcf_sql_moneycf_sql_money4cf_sql_ncharcf_sql_nvarcharcf_sql_numericcf_sql_realcf_sql_refcursorcf_sql_smallintcf_sql_sqlxmlcf_sql_timecf_sql_timestampcf_sql_tinyintcf_sql_varcharbigintbitcharblobclobnclobdatedecimaldoublefloatidstampintegerlongvarcharlongnvarcharmoneymoney4ncharnvarcharnumericrealrefcursorsmallintsqlxmltimetimestamptinyintvarchar
maxlength numeric
scale numeric
Default: 0
0
CF_SQL_NUMERIC and CF_SQL_DECIMAL. null boolean
NULL value. Yes: ignores the value attribute and passes NULL No: passes the value attribute list boolean
separator string
Compatibility
ColdFusion:
cfsqltype values, prior versions treated invalid types as varchar - see CF-4198194 for reference. Lucee:
int and cf_sql_int for integer Railo:
int and cf_sql_int for integer BoxLang:
<bx:queryparam> in BoxLang Links more information about cfqueryparam
- Mastering cfqueryparam - A guide to understanding all the ins and outs of using cfqueryparam.
Examples Sample code using the cfqueryparam tag
Shows how to use a cfqueryparam tag within cfquery.
<cfquery name="news">
SELECT id,title,story
FROM news
WHERE id = <cfqueryparam value="#url.id#" cfsqltype="cf_sql_integer">
</cfquery> Assumes url.idList is a comma separated list of integers, eg: 1,2,3
<cfquery name="news">
SELECT id,title,story
FROM news
WHERE id IN (<cfqueryparam value="#url.idList#" cfsqltype="cf_sql_integer" list="true">)
</cfquery> Shows a basic example of using an expression to control whether null is passed to the queryparam
<cfquery name="test">
INSERT into test ( key, value )
VALUES(
<cfqueyparam value="#key#" cfsqltype="cf_sql_varchar" null="#isNumeric(Key) EQ false#">
<cfqueryparam value="#value#" cfsqltype="cf_sql_varchar" null="#value EQ ''#">
)
</cfquery> CF 11+ script syntax using queryExecute and struct notation
exampleData = queryNew("id,title","integer,varchar",[{"id":1,"title":"Dewey defeats Truman"},{"id":2,"title":"Man walks on Moon"}]);
result = queryExecute(
"SELECT title FROM exampleData WHERE id = :id",
{ id = 2 },
{ dbtype="query" }
);
writeOutput( result.title[1] ); Expected Result: Man walks on Moon
CF 11+ script syntax using queryExecute and struct notation for multiple parameters
exampleData = queryNew("id,title","integer,varchar",[{"id":1,"title":"Dewey defeats Truman"},{"id":2,"title":"Man walks on Moon"}]);
result = queryExecute(
"SELECT * FROM exampleData WHERE id = :id AND title = :title",
{
title={value="Man walks on Moon", cfsqltype="cf_sql_varchar"}, id={value=2, cfsqltype="cf_sql_integer"}
},
{ dbtype="query" }
);
writeOutput(result.title[1]); Expected Result: Man walks on Moon
CF 11+ script syntax using queryExecute and full array notation
exampleData = queryNew("id,title","integer,varchar",[{"id":1,"title":"Dewey defeats Truman"},{"id":2,"title":"Man walks on Moon"}]);
result = queryExecute(
"SELECT title FROM exampleData WHERE id = ?",
[
{ value=2, cfsqltype="cf_sql_varchar" }
],
{ dbtype="query" }
);
writeOutput( result.title[1] ); Expected Result: Man walks on Moon
CF 11+ script syntax using queryExecute and array shorthand
exampleData = queryNew("id,title","integer,varchar",[{"id":1,"title":"Dewey defeats Truman"},{"id":2,"title":"Man walks on Moon"}]);
result = queryExecute(
"SELECT title FROM exampleData WHERE id = ?",
[ 2 ],
{ dbtype="query" }
);
writeOutput( result.title[1] ); Expected Result: Man walks on Moon
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/cfqueryparam