Posted in ColdFusion | Posted on 05-30-2008 | 4,855 views
This is a dupe, but as it comes up kind of often, I thought I'd blog it anyway. Doug asks:
After I perform a cfquery, is there a way to see what the query actually looked like? I'm not asking if there was an error, I know I can grab that through a cfcatch. But if the query was good (and I'm still not getting the results I expected) I want to see how the query was formed. I know I can great a variable and copy/paste the query contents into that, but I didn't know if there was some kind of easier way to just look at the query or something.
Absolutely. Since CF7 we've had two ways to introspect ColdFusion queries. To answer your main question, you can get the SQL (along with other bits of info) by using result= in your query:
2select #limit# id, title
3select id, title
4from tblblogentries
5where title like <cfqueryparam cfsqltype="cf_sql_varchar" value="%foo%">
6limit #limit#
7</cfquery>
This will create a structure named result with these values:
- Cached: Boolean that tells you if the query was cached
- ColumnList: List of columns. This is NOT in the same order as your query. (More on that in a bit.)
- ExecutionTime: Time it took to run the query.
- RecordCount: Number of rows returned
- SQL: The SQL, what you want!
- SQLParameters: An array of values used in cfqueryparameter tags. Note that the SQL value will use a ? for each of these.
Another function you can use is getMetaData(). If you use it on a query you get an array of structs back for each column. This is in the order specified in your SQL, and even matches the case used in the SQL if you care about that. It contains a type name that tells you what type of column it is as well as a "IsCaseSensitive" flag. So if you do care about what order you selected your columns, this is what you would use.


If he wants it for debugging, you can just dump it. Even easier.
I was working on a fairly complicated query awhile back. It had a bunch of LEFT JOINS and was pulling data from multiple tables. It wasn't throwing and error, but it wasn't returning the results I expected, so I was looking for a way to see the SQL string that was generated.
Also, most of my clients are on shared hosting, so I don't have access to any ColdFusion admin stuff.
Thanks Ray, you made my Friday!
Like that
<cfquery>
"the query"
<cfabort>
</cfquery>
That is of course if you just want to have what was generated between the cfquery tags
also you can add a <pre></pre> around that, to keep to formating.
It's useful for generating SQL scripts and other automation type tasks like Ray mentioned.
When I CFDUMP it I get the field name, the data type, and in the order it appears in the table (unlike a CFDUMP of a query which alpha sorts the columns). However, what I also need is the size of the field - varchar(50), char(6), etc...
I can see that it is not in the meta data. Is there another way to retrieve this?
Thank you!
SELECT column_name 'Column Name',
data_type 'Data Type',
CHARacter_maximum_length 'Maximum Length'
FROM information_schema.columns
WHERE table_name = 'table-name-here'
Couple that with a list of all of your tables:
select TABLE_NAME from INFORMATION_SCHEMA.[TABLES]
and you've got a quick and dirty inspector to look up your tables, field names, data types and sizes.
Thanks
[Add Comment] [Subscribe to Comments]