I’ve created this code blow that works and does what I want it to do, however I was wondering if there is a better smarter way to do this. Here is the code:
<cfset myList = ValueList(qSeminarWisReg.SeminarWisTitle,",")>
<cfif ListLen(myList,",") lte 1>
<cfquery datasource="#APPLICATION.dataSource#" name="qSeminarWisRegTwo">
SELECT * FROM SeminarWis
WHERE SeminarWisID = <cfqueryparam value="#myList#" cfsqltype="cf_sql_integer">
</cfquery>
<cfelse>
<cfquery datasource="#APPLICATION.dataSource#" name="qSeminarWisRegTwo">
SELECT * FROM SeminarWis
WHERE 0 = 0
AND (SeminarWisID = <cfqueryparam value="100000000" cfsqltype="cf_sql_integer">
<cfloop index = "x" list = "#myList#" delimiters = ","> OR SeminarWisID = #x#</cfloop>)
</cfquery>
</cfif>
Basically I have a dynamically generated list, which has numbers in it, delimited by a comma. Now these numbers correspond to Unique Keys in a table, so then I’m just dynamically grabbing all the rows the user wants.
Like I said, this works, but if anyone has any tips on how to make this better let me know! The part that bothers me is that I have to use the “1000000” value (one that doesn’t exist) before i can get into the OR statements.
I think you want to use CFQUERYPARAM with list=”true” and the IN operator:
Note that this will only work if listLen(myList) GTE 1