My program generates a list at runtime of values. I need to send a sql query that looks in a table for entries that have a column that contain one of the values in the list. I can’t use the usual chain of OR’s because I don’t know how big the list will be. Is there a nice way to use an array or some IEnumerable to build a SQL statement that makes a big chain of OR’s for me? Using C# BTW
I’m using SQL Server but I’d prefer something that works across all databases if such a thing exists.
Thanks!
Try this:
And make sure you turn @param into the form “‘@p1, @p2, @p3′” by using your chosen language’s implode function.
The query should be standard for all SQLs I know.
Alternatively if you want to do a LIKE comparison:
@param is still a quoted string, but you need to use a different COALLESCE operator depending on your database (SQL Server: ‘+’, MySQL: ‘.’, Oracle: ‘|’).
Example PHP implode code:
Example C# “implode” code: