In my ColdFusion 11 app, with SQL Server 2008-R2, I've following cfquery tag inside a CF Component:
<cfquery name="result_set" dataSource="#request.dsn_name#">
select name, state from myTable #REReplace(where_clause,"''","'","ALL")#
</cfquery>
Here where_clause
is a variable. The CF replaces one single quote with two and hence I'm using the REReplace function to replace two single quotes back into one. So my query changes, e.g. from
select name, state from myTable WHERE name IN (''ABC'')
to this:
select name, state from myTable WHERE name IN ('ABC')
The problem is when a name column value contains a single quote as well. E.g.
select name, state from myTable WHERE name IN ('Smith's bat')
In such cases the query fails. How can I resolve such cases. I tried PreserveSingleQuotes but it has the same issue where column has values with single quotes.
UPDATE
This app was developed years ago by someone using ColdFusion MX 7. The original author is creating dynamic string for where_clause variable based on certain conditions. It's a long cfs file with several conditions used for creating dynamic string for where_clause. Hence, using a cfqueryparam may either not be suitable or may require a complete overhaul of the code that customer will not allow.