All the existing answers/comments seem to touch on different aspects, so this is an attempt to consolidate all this information into one clear explanation.
(This answer is set to community wiki - please edit as appropriate.)
To access a variable from a query, use:
QueryName.ColumnName[RowNum]
QueryName["ColumnName"][RowNum]
These will both work at any point after the query is created.
Both can be used inside cfoutput
to display the variable, or inside cfset
to assign the variable.
The second one is useful for dynamic variables, and can accept variables like so:
QueryName[DynamicColumnName][RowNum]
QueryName["Partial#DynamicName#"][RowNum]
QueryName["Partial"&DynamicName][RowNum]
As a convenience, instead of looping through a query manually:
<cfloop index="CurrentRow" from="1" to="#QueryName.RecordCount#">
<cfoutput>#QueryName.ColumnName[CurrentRow]#</cfoutput>
</cfloop>
You can simply do:
<cfloop query="QueryName">
<cfoutput>#QueryName.ColumnName[CurrentRow]#</cfoutput>
</cfloop>
And as a further shortcut, you can do:
<cfoutput query="QueryName">
#QueryName.ColumnName[CurrentRow]#
</cfoutput>
And, when inside a <cfloop query="">
or a <cfoutput query="">
you can simply do:
<cfoutput query="QueryName">
#ColumnName#
</cfoutput>
However, this last shortcut is only for display - if you do:
<cfset ColumnName = "NewValue" />
This will not modify the original query data. Instead, you need to do:
<cfset QueryName.ColumnName[CurrentRow] = "NewValue" />
And that will modify the results of the query (but not the value in the database).
To display the actual SQL query that has been run, with CF8 (also Railo and OpenBD), you can do:
<cfdump var="#QueryName#"/>
And it will show you both the results of the query and the actual SQL which has been run.
To display the actual SQL with CF7, you need to add a result="QueryInfo"
to your cfquery
tag, then <cfdump var="#QueryInfo#"/>
will show you the SQL.