Skip to main content
Inspiring
June 7, 2010
Question

Creating a list that SQL can search using IN

  • June 7, 2010
  • 1 reply
  • 3662 views

I have to create a list from a previous query and also append another value to the list.

To test I created the list manually, with some basic code

<CFSET country_list="">
<CFSET country_list = listappend(country_list,'''AU''')>
<CFSET country_list = listappend(country_list,'''99''')>

And then ran the query to search for records that contain either AU or 99

SELECT DISTINCT(campcountry_camp_uid)
FROM campaign_countries
WHERE campcountry_country_short IN ('#country_list#')

However, I'm having trouble with the SQL because it does not like the way I have created thelist, so a CFDUMP shows this

SELECT DISTINCT(campcountry_camp_uid) FROM campaign_countries WHERE campcountry_country_short IN ('''AU'',''99''')

This does not retrieve any records. I've tried all sorts of ways of creating the list and tweaking the SQL but can't seem to get it right.

Appreciate any pointers

Thanks

Mark

This topic has been closed for replies.

1 reply

ilssac
Inspiring
June 7, 2010

Have you tried this one:

<CFSET country_list="">
<CFSET country_list = listappend(country_list,''U')>
<CFSET country_list = listappend(country_list,'99')>

SELECT DISTINCT(campcountry_camp_uid)
FROM campaign_countries
WHERE campcountry_country_short IN (<cfqueryParam value="#country_list#" list="yes" cfsqltype="cf_sql_varchar">)

ACS LLCAuthor
Inspiring
June 7, 2010

Thanks Ian, you hit the nail on the head right away

I didn't realize what I was getting into with this, I thought it was as simple as passing it the necessary info, even when it looked like it was in the same format 'AU','99' by CFOUTPUT, which was the same as hard coding it into the SQL it did not work, strange.

I just need to put a query in with a variable I add to the list using code, which I don't think will upset what you did, and I'm good

Glad I broke down the query and tested each part as this is a query, within a query, within a query!

Thanks

Mark

ilssac
Inspiring
June 7, 2010

ACS LLC wrote:

I just need to put a query in with a variable I add to the list using code, which I don't think will upset what you did, and I'm good

You will probably want to be aware of the valueList() function.  It is very useful to turn a query column into a comma delimited list.

ACS LLC wrote:

Glad I broke down the query and tested each part as this is a query, within a query, within a query!

YUCK!  It is constructs like that which give CFML such a bad name in some programmers circles.  I would give it a 98% chance that the code could be refractored to remove the nested query ugliness.