Skip to main content
Known Participant
July 2, 2007
Question

Checking for an empty query

  • July 2, 2007
  • 1 reply
  • 2609 views
Hi guys,

I wanted to know if it was possible to check a query to see if it is empty or not (or if the value comes out to be true or false). I hope I'm understandable.

Would I use a IsBoolean or IsVaild function?
This topic has been closed for replies.

1 reply

Inspiring
July 2, 2007
Typically, you can check the query.RecordCount to see if it contains one or more.

<cfquery name="qTest" datasource="dsn">
select * from mytable
</cfquery>

<cfif qTest.RecordCount GTE 1>
//code
</cfif>

As long as the query itself does not fail (error, exception, etc.), you will get a query variable back and can test it's record count.

If there is a concern that the query might fail (perhaps the SQL is dynamic in some way), you can use cftry/cfcatch to ensure that, at the least, you get back an empty query:

<cftry>
<cfquery name="qTest" datasource="dsn">
select * from mytable
</cfquery>
<cfcatch type="database">
<cfset qTest = QueryNew("column1,column2")/>
</cfcatch>
</cftry>

<cfif qTest.RecordCount GTE 1>
//code
</cfif>

Hope this helps...