Whatever message this page gives is out now! Go check it out!

QueryKeyExists

Last update:
May 18, 2026

Description

This function checks the query for a column with the same name as the key provided.

Returns

True if a matching column exists, false otherwise.
Category
History
ColdFusion (2018 release): Introduced named parameters.
New in Adobe ColdFusion (2016 release)
See also

Syntax

boolean QueryKeyExists(Object query, String column)

Parameters

Parameter
Description
query
(Required) Query to be checked whether a key is available.
column
(Required) Key to be matched with the columns.

Example 1

<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32} 
                ]); 
    status=QueryKeyExists(myquery,"amount");
       if (status=="YES"){
             writeOutput("Matching column exists: "& status);
       }
       else{
             writeOutput("Matching column exists: "& status);
       }
</cfscript>

Example 2

<cfquery 
       name="courses" datasource="cfdocexamples" 
       cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#"> 
       SELECT CORNUMBER,DEPT_ID,COURSE_ID,CORNAME 
       FROM COURSELIST 
</cfquery> 

<cfscript> 
    //Use an absolute path for the files. -
    theDir=GetDirectoryFromPath(GetCurrentTemplatePath()); 
    theFile=theDir & "courses.xls"; 
    //Create two empty ColdFusion spreadsheet objects
    theSheet = SpreadsheetNew("CourseData"); 
   SpreadsheetAddRows(theSheet,courses); 
    
</cfscript> 
 
<!--- Write the two sheets to a single file ---> 
<cfspreadsheet action="write" filename="#theFile#" name="theSheet"  
    sheetname="courses" overwrite=true> 
<cfspreadsheet action="read" src="#theFile#" sheetname="courses" query="queryData"> 

<cfoutput>querydata.KeyExists("col_1"):#querydata.KeyExists("col_1")#</cfoutput>

Using member function

<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       status=myResult.keyexists("LOCATION");
       if (status=="YES"){
             WriteOutput("Matching column exists: "& status);
       }
       else{
             WriteOutput("Matching column exists: "& status);
       }
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page