March 2nd, 2005, 01:03 PM
check if recordset exists?
how do i check if a recordset exists? i can't use isDefined() that wont work for queries. can't user .recordcount or len() because they assume and the recordset exists in the first place. any ideas?
March 2nd, 2005, 02:00 PM
<cfif queryName.recordCount gt 0>
record set exists
March 2nd, 2005, 02:10 PM
isDefined() will certainly work on a query name. You can also do:
<cfif structKeyExists( variables, 'myQueryName' )>
(assuming the query is in the variables scope of course).
March 3rd, 2005, 09:31 AM
but isDefined() did NOT work, i got an error saying isDefined() will not work on complex variables, such as a recordset. and again, .recordcount did NOT work because that assumes the recordset exists in the first place. if the recordset does not exist it'll blow up saying ".recordcount is not defined in variable [insert name here]" or something along those lines
March 3rd, 2005, 09:49 AM
You might try being a bit less emphatic, because I am telling you that you are doing something wrong. I think what you mean is "I can't make it work" or "It's not working for me". This code works fine and both conditions are met.
I recommend using structKeyExists() instead of isDefined() because it is more specific (it's forcing a lookup in only the variables scope, while isDefined() will automatically look for the variable in any of the auto-lookup scopes beyond the variables scope).
<cfquery name="qryGetCatalog" datasource="wegotwidgets">
order by productName
<cfif isDefined( 'qryGetCatalog' )>
<cfif structKeyExists( variables, 'qryGetCatalog' )>
the struct key exists
Last edited by kiteless; March 3rd, 2005 at 09:54 AM.
March 3rd, 2005, 12:07 PM
December 4th, 2012, 05:17 AM
Reason was Quotes
Just to clear things for future readers: this is a common error coldfusion programmers do - forgetting the quotes. I do not want to test if the variable in the content of qryGetCatalog exists, but the variable "qryGetCatalog" itself, so quotes are needed.