Search code examples
coldfusioncoldfusion-9

Check query results if no data is returned


I have a query that I am calling to update an email service. Most times it will have data in it, but in testing I came across the situation of it not returning any data because there was no data to return. In the case of no data it returns the error "Variable EDITEDACCTS is undefined".

I have tried wrapping the query in a <cftry> but it doesn't "fail" per se so it does not trip the <cfcatch>. I have also tried defining the variable

var EditedAccts = QueryNew("")

as well as simply trying

<cfif NOT isDefined(#EditedAccts#)>

and it always returns "Variable EDITEDACCTS is undefined".
I need a production ready solution to this and I'm hoping somewhere here on SO can help me out.

Thanks in advance for your help.


Solution

  • I have just found the answer. You set the "result" parameter in the query call and then you can check the recordcount field returned.

    <cfquery name="EditedAccts" datasource="mydatasource" result="queryResult">
        ...query goes here...
    </cfquery>
    

    When using the "result" parameter you get a struct returned with the sql used, the cached setting, the execution time and the record count. Now I can check the record count and proceed from there.

    Hopefully this will help someone in the future.