I’m currently working on a query to search a specific table. The requirements say that I need to grab all records where any of a group of fields are like the input value. As such I have a query that looks similar to
SELECT * FROM MyTable
WHERE Col1 like @input
or Col2 like @input
or Col3 like @input
There could be any number of columns in the where clause. One particular customer is going to have 50 columns. As such, this where clause is going to get fairly expensive.
Is there another syntax or method that can be used so that I could say “does the input value exist in any of x columns?”
Would you write the query differently?
Depending on which and where wildcards are allowed these queries could be expensive indeed, because the optimizer will have trouble using indexes on many of them to speed them up.
If you are running this on MS SQL Server you might consider using the Fulltext searching features and the CONTAINS keyword to implement this.
Or to search all text-indexed columns