Passing querytext as a function argument OR When is a string not a string?

Today I wrote a function that added in default rows to a query. Part of the function needed the text of a query to find the default values. I attempted to pass in the query text and use the same text in a <cfquery> tag.

This did not work.

Here is the query I passed in:

SELECT This, That FROM Those WHERE bar = 'foo' ORDER BY SortOrder

Here is the resulting error:

Error: [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near 'Foo'.

After a few minutes, I figured out the query worked fine if I left the criteria out. This worked:

SELECT This, That FROM Those ORDER BY SortOrder

Well, it kinda worked. It parsed and executed fine but I obviously got more records than I wanted. I expected it was a wierd parsing error and then used chr() function to replace the single quotes AND the equal sign. Nada.

The final product that worked with the criteria was to use toString(). <cfsavecontent> would probably work also.

<cfquery name="ColumnQuery" datasource="#DSN#">
#toString( arguments.SQLForRowTypes)#
</cfquery>

It was not obvious this wouldn't work without the toString(). I could dump the arguments scope and see the query text VERBATIM. I could also copy the cfdumped text and paste it directly in QueryAnalyzer and it also ran. I am still scratching my head over this one.

Comments
Isn't this what preserveSingleQuotes() is for?

http://cfquickdocs.com/?getDoc=PreserveSingleQuote...
# Posted By Sean Corfield | 3/22/07 9:51 PM
@Sean,


You are correct sir!

Due to my nearly rabid use of cfqueryparam, I'd placed preserveSingleQuotes on the back burner. Thanks for reminding me.

dw

From the Docs:

Prevents ColdFusion from automatically escaping single-quotation mark characters that are contained in a variable. ColdFusion does not evaluate the argument.
# Posted By Dan Wilson | 3/23/07 1:19 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.001. Contact Blog Owner