How can I get the SQL from a query object in ColdFusion?

Kip picture Kip · Nov 17, 2010 · Viewed 25.7k times · Source

How can I get the SQL used to generate a cfquery object? If I cfdump the object, it shows it having an "SQL" property, which contains the actual query. Turning on debugging won't help me because I am making an API call, so output is not HTML and debug info would break it. I'm just trying to debug exactly what query is being executed.

<cfquery name="tableElements" datasource="TestSQLServer">
SELECT * FROM tableElements
</cfquery>

<cfdump var="#tableElements#" /> <!--- Shows object having "SQL" property --->
<cfoutput>SQL: #tableElements.SQL#</cfoutput> <!--- Error: Element SQL is undefined in TABLEELEMENTS. --->

Answer

Todd Sharp picture Todd Sharp · Nov 17, 2010

Add a 'result' attribute to your cfquery. The SQL is in the result struct, not the query variable.