Question
Feeding a list into a query
Hi,
I have a simple table that contains two columns (StaffID, Code). Both take integer values. I now have a list of StaffIDs that I want to pull up. How can I feed this list into a query. When I tried the following, I got an error because the list is a string while the query is expecting integer values.
How to correct the above? I know I can do the following, but it's a bit unwieldy.
Thanks.
Min
I have a simple table that contains two columns (StaffID, Code). Both take integer values. I now have a list of StaffIDs that I want to pull up. How can I feed this list into a query. When I tried the following, I got an error because the list is a string while the query is expecting integer values.
quote:
<Cfset idList = "1,5,6,10">
<cfquery name="test" datasource="myDB">
select Code
from StaffTable
where StaffID in (#idList#)
</cfquery>
How to correct the above? I know I can do the following, but it's a bit unwieldy.
quote:
<Cfset idList = "1,5,6,10">
<cfquery name="test" datasource="myDB">
select Code
from StaffTable
where StaffID = listGetAt(idList,1)
<cfloop from="2" to="#ListLen(idList)#" index="idx">
or StaffID = #listGetAt(idList,idx)#
</cfloop>
</cfquery>
Thanks.
Min
