chapter31

development in a land far far away…

at the moment

History is not what happened. History is what was written down.

A great feature of <cfquery> which was introduced in CFMX7 is the addition of the ‘result’ attribute. When used, a developer can access a Structure of query result variables including the following keys:

  • Cache
  • ColumnList
  • ExecutionTime
  • RecordCount
  • SQL
  • SQLParameters

You can read more about the ‘result’ attribute in a previous post or at Live docs.

One thing I’ve come across is that the structure returned (by using the result attribute) is created where the query is executed and the value of ‘result’ references this struct in the local variable scope.

So an inline query followed by a reference to the ‘result’ struct would run fine:

But a problem exists when you have a query in a Component that is being returned by a method call. In this case the structure won’t exist in the calling templates ‘local’ scope and an exception will be thrown.

stResult exception

The simple way around this is to declare the ‘result’ attribute in the request scope:

Now you can reference the structure inside the calling template using the request scope:


Related Pages

5 Responses to “cfquery ‘result’ attribute caveat”

  1. —– In the CFC cfquery tag

    ———————-

    This is just an example, you could certainly set a result name for each function/cfquery which would persist in the object instance.

    The other option would be to combine the returned result and query as a deep structure

    Christopher Wigginton

  2. bummer, there was some nice example code, but the blog stripped out the comments rather than encode the output. oh well. The gist of it was to use result=”this.result” which sets a property of the cfc which persists with the object instance.

    Christopher Wigginton

  3. Nice find - works like a charm - thanks.

    Derek Versteegen

  4. Another tip, when you’re viewing the SQL, wrap it in pre tags to format it as it was typed in the cfquery tags.

    Adrian Lynch

  5. Nice tip Adrian, thanks

    Michael Sharman

Leave a Reply