User Guide Cancel

QueryKeyExists

 

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

Query functions

History

ColdFusion (2018 release): Introduced named parameters.

New in Adobe ColdFusion (2016 release)

See also

QuerySortQueryFilterQueryEach

Syntax

boolean QueryKeyExists(Object query, String column)
boolean QueryKeyExists(Object query, String column)
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>
<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>
<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>
<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>
<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>
<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>
<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>

Get help faster and easier

New user?