#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    199
    Rep Power
    0

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    34
    Rep Power
    10
    <cfif queryName.recordCount gt 0>

    record set exists

    </cfif>
  4. #3
  5. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,275
    Rep Power
    968
    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).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    199
    Rep Power
    0
    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
  8. #5
  9. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,275
    Rep Power
    968
    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.

    Code:
    <cfquery name="qryGetCatalog" datasource="wegotwidgets">
      select productID,
             productName,
             productDescription,
             imageFile,
             price
        from Products
    order by productName
     </cfquery>
    
    <cfif isDefined( 'qryGetCatalog' )>
    its defined
    </cfif>
    <br><br>
    <cfif structKeyExists( variables, 'qryGetCatalog' )>
    the struct key exists
    </cfif>
    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).
    Last edited by kiteless; March 3rd, 2005 at 09:54 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    199
    Rep Power
    0
    oh ok i see... thanks!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    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.

IMN logo majestic logo threadwatch logo seochat tools logo