You can also use CFTRY/CFCATCH... which will do two things for you; 1) it will wrap the debug message(s) up so that if no error is thrown, you won't have to see any extraneous output, and 2) it will expose several new CFCATCH properties that do exactly what you want.
This is almost directly from the CF documentation on Exception Handling... but I use it all the time:
Code:
<CFTRY>
<CFQUERY name="myquery" datasource="mydsn">
SELECT foobar from MYTABLE
</CFQUERY>
<CFCATCH TYPE="database">
<CFOUTPUT>
<P><B>DATABASE ERROR</B><BR />
<UL>
<LI><B>Message:</B> #CFCATCH.Message#
<LI><B>Native error code:</B> #CFCATCH.NativeErrorCode#
<LI><B>SQLState:</B> #CFCATCH.SQLState#
<LI><B>Detail:</B> #CFCATCH.Detail#
</UL>
</CFOUTPUT>
</CFCATCH>
</CFTRY>
The CFCATCH.NativeErrorCode and CFCATCH.SQLState properties may or may not contain anything depending on what database type and driver you're using. But
CFCATCH.Detail will almost always contain, amoung other things, the SQL statement that threw the error.
Other little known debugging methods are:
to turn debugging on for an individual page, add "
mode=debug" to the end of the URL (ie - mydomain.com/mydir/mypage.cfm
?mode=debug).
to turn debugging on for an individual query, add the parameter "
DEBUG" to the open CFQUERY tag (ie - <CFQUERY name="myquery" datasource="mydsn"
DEBUG>)
Both of these work no matter whether debugging is turned on in CF admin or not (if not, you should be able to implicitly tell CF to display it by using <CFSETTING SHOWDEBUGOUTPUT="Yes">)
Hope it helps,
-Carl