How to SELECT a column IN a list

I have something like this:

<cfset lRegIDs = "C3AC20B4-3048-2F2D-064489365ED76B91XX,6A11F0D9-3048-2F2D-061732F9BB2EC303">

<cfquery name="qGetList" datasource="mydsn">
Select first_name,last_name,email,reg_id  from tbl
where 0 = 0
AND reg_id IN ('#lRegIDs#')
order by last_name, first_name, email ASC

This doesnt work. How can I "optimally" loop thru the list and select the records
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CMI_ITAuthor Commented:
The list of reg_ids, is about 350.
Jinesh KamdarCommented:
Iguess in ur case, the only option (since ur individual values are so lengthy) is to load them all into a table and then check for these values in the table rather than comapre against 350 literal vaues. You can't compare the IN operator against a single string containing these values separated by commas b'coz its still just a single string value.
try this
Select first_name,last_name,email,reg_id  from tbl
#lRegIDs# LIKE reg_id + ',%'  OR
#lRegIDs# LIKE '%,' + reg_id + ',%' OR
#lRegIDs# LIKE '%,' + reg_id OR
#lRegIDs# = reg_id
order by last_name, first_name, email ASC 

Open in new window

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Use cfqueryparam with the list attribute.  Change the cfsqltype if needed

<cfquery name="qGetList" datasource="mydsn">
Select first_name,last_name,email,reg_id  from tbl
where reg_id IN
<cfqueryparam value="#lRegIDs#" cfsqltype="cf_sql_varchar"  LIST="true">
order by last_name, first_name, email ASC
CMI_ITAuthor Commented:
This looks good but its does not quite the list items so it does not return a full recordset

I ended up doing something like this:

    <cfquery name="qGetList1" datasource="mydsn">
    Select first_name,last_name,email,reg_id  from #tbl#
    where 0 = 0
    AND reg_id IN (<cfloop list="#lRegIDs#" index="i">'#trim(i)#'<cfset cnt=cnt+1><cfif cnt NEQ ListLen(lRegIDs)>,</cfif></cfloop>)

Rather than looping through the list of RegIDs I would recommend taking a step back and look at creating a query that incorporates the query that populated lRegID with this query, so that you are only running one query and the join is done by the database instead of a very inefficient IN statement.

If you can't do that then you need to use <cfqueryparam> as suggested by agx.

An alternate solution to this is to use ListQualify() to set the qualifiers on your list.
eg. AND reg_id in (#ListQualify(lRegID,"'")#)
NOTE:  the quotes in ListQualify are double quotes surrounding a single quote.

This will work, but I would recommend looking at creating one query first and then cfqueryparam before this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.