Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I get the SQL used to generate a query?

Most queries written in ColdFusion will contain one or more dynamic portions. If you want to get the SQL that was actually passed to the database, use the result attribute of the <cfquery> tag:

<cfset name = "e">

<cfquery name="getIt" datasource="cfartgallery" result="result">
select artistid
from artists
where lastname like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#name#%" maxlength="255">
</cfquery>

<cfdump var="#result#">

The result attribute returns a structure with keys that relate to the query. The keys that you would be concerned about our SQL and SQLParameters. The SQL key will contain the sql that was passed to the database. Each and every <cfqueryparam> tag that was used will be replaced with a question mark. The SQLParemeters key will contain an array of values that correspond to each question mark.


This question was written by Raymond Camden.
It was last updated on May 20, 2006 at 3:11:44 PM EDT.

CFML Referenced

<cfquery>
<cfqueryparam>

Categories

Database / SQL

Comments

There are no comments for this entry.