Skip to main content
Inspiring
April 20, 2008
Answered

Forming a SQL IN expression

  • April 20, 2008
  • 1 reply
  • 521 views
If I use a hard-coded set of string values in an IN expression, the query returns the expected data.

If I then replace the hard-coded expression with an equivalent CF variable embedded in a cfqueryparam tag, the query executes but returns no records. A cfdump of the CF variable indicates it is exactly the correct pair of single-quoted values that were previously hard-coded.

A cfdump of the query result set shows the IN expression is a single row array containing 'a','b'

Any ideas what might be wrong?

Jim

This topic has been closed for replies.
Correct answer Jim_Gibson_sbswa_com
Jim Gibson sbswa.com
I tried directly assigning the CF variable just before the query, to make sure there are no surprises:
<cfset categoryList = "'a','b'"/>


When using cfqueryparam you should not need to single quote varchar values. I think the single quotes are skewing your results. The resulting query is probably looking for a category that literally equals 'a' or 'b' (including the single quotes).

Remove the single quotes and the query should work as expected.
<cfset categoryList = "a,b"/>

That took care of it. Thank you very much! Using the cfqueryparam "list" attribute, and using a list of unquoted column values solved the problem. I also noted that the unquoted list needs to contain no spaces, just comma separators. I am now getting the expected records back.

1 reply

Inspiring
April 20, 2008
You need to use cfqueryparam's "list" attribute to indicate the parameter contains multiple values. When you do not use list="true", then 'a,b' is treated as a single value. The resulting query is equivalent to saying:

SELECT * FROM email WHERE category = 'a,b'

Since your table probably does not contain any records where the category equals "a,b", the query returns zero records.

Inspiring
April 20, 2008
Thanks. The list attribute definitely changed the expression, which now shows up in the cfdump as (?,?) with a SQLPARAMETERS array of two separate strings 'a' and 'b'. However, zero records are still returned even though everything looks perfect. I tried directly assigning the CF variable just before the query, to make sure there are no surprises:
<cfset categoryList = "'a','b'"/>
and then execute the query with the following. Looks perfect, but no records. If I then replace the <cfqueryparam> tag with the hardcoded list 'a','b' the query returns three records as expected. I am using an MS Access database. Any possible quirks with <cfqueryparam> doing this transaction with that driver?

Inspiring
April 20, 2008
Jim Gibson sbswa.com
I tried directly assigning the CF variable just before the query, to make sure there are no surprises:
<cfset categoryList = "'a','b'"/>


When using cfqueryparam you should not need to single quote varchar values. I think the single quotes are skewing your results. The resulting query is probably looking for a category that literally equals 'a' or 'b' (including the single quotes).

Remove the single quotes and the query should work as expected.
<cfset categoryList = "a,b"/>