Variable as SQL in cfquery

2019-01-29 10:06发布

问题:

I have a table that is storing SQL as one of its elements. I need to be able to run that stored SQL query, but can't figure out how to do it. So far, I'm using the following method:

<cfquery name="qDynamicTag" datasource="#myDSN#">
#PreserveSingleQuotes(arguments.sql)#
</cfquery>

If I dump the value of #PreserveSingleQuotes(arguments.sql)# before executing the cfquery I see the correct SQL statement. However, when I try to run the above cfquery I get a SQLException with the message:

Syntax error at token 0, line 0 offset 0

An example of one of the SQL statements that I'm trying to run:

select productid from catalog.producttag where tag = 'case' or tag = 'cases'

Any ideas on what I'm doing wrong?

回答1:

Ha! Thanks for all your comments. The suggestion to check the debugging output of the DB activity made me realize that I wasn't seeing the DB activity in the debug section of the page because the query was executing through an AJAX call. After calling the page with the query directly, the DB debug output revealed that the code was good, but the data in the database was bad. One of the records with SQL didn't exist. Thus, cfquery was trying to run a blank string.