
441
DEVELOPING
COLDFUSION 9 APPLICATIONS
Accessing and Using Data
Las
t
upda
te
d 8/5/2010
Escaping wildcards
You can specify your own escape character by using the conditional ESCAPE clause.
Example
The following example uses the ESCAPE clause to enable a search for a literal percent sign (%), which ColdFusion
normally interprets as a wildcard character:
SELECT emp_discount
FROM Benefits
WHERE emp_discount LIKE '10\%'
ESCAPE '\';
Managing data types for columns
A Query of Queries requires that every column has metadata that defines the data type of the column. All queries that
ColdFusion creates have metadata. However, a query created with
QueryNew
function that omits the second parameter
does not contain metadata. You use this optional second parameter to define the data type of each column in the query.
Specify column data types in the QueryNew function
❖
Type a
QueryNew
function, specifying the column names in the first parameter and the data types in the second
parameter, as the following example shows:
<cfset qInstruments = queryNew("name, instrument, years_playing", "CF_SQL_VARCHAR,
CF_SQL_VARCHAR, CF_SQL_INTEGER")>
Note:
To see the metadata for a Query of Queries, use the GetMetaData function.
Specify the column data types in the QueryAddColumn function
1
Create a query by specifying the QueryNew function with no parameters.
<!--- Make a query. --->
<cfset myQuery = QueryNew("")>
2
Add and populate a column with the QueryAddColumn function, specifying the data type in the third parameter:
<!--- Create an array. --->
<cfset FastFoodArray = ArrayNew(1)>
<cfset FastFoodArray[1] = "French Fries">
<cfset FastFoodArray[2] = "Hot Dogs">
<cfset FastFoodArray[3] = "Fried Clams">
<cfset FastFoodArray[4] = "Thick Shakes">
<!--- Use the array to add a column to the query. --->
<cfset nColumnNumber = QueryAddColumn(myQuery, "FastFood", "CF_SQL_VARCHAR",
FastFoodArray)>
If you do not specify the data type, ColdFusion examines the first 50 rows of each column to determine the data type
when performing conditional expressions.
In some cases, ColdFusion can guess a data type that is inappropriate for your application. In particular, if you use
columns in a WHERE clause or other conditional expression, the data types must be compatible. If they are not
compatible, use the CAST function to recast one of the columns to a compatible data type. For more information on
casting, see “
Using the CAST function
” on page 442. For more information on data type compatibility, see
“
Understanding Query of Queries processing
” on page 448.
Note:
Specifying the data type in the
QueryNew
function helps you avoid compatibility issues.
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...