I have an ASP.NET application which needs to search a VARCHAR(20) column using lists and ranges. In other words, the input could be something like:
ABC,444,CD-EF,90-BA,HIJ
and the result needs to be the equivalent of:
SELECT * FROM table
WHERE
Col1 = 'ABC' OR
Col1 = '444' OR
Col1 BETWEEN 'CD' AND 'EF' OR
Col1 BETWEEN '90' AND 'BA' OR
Col1 = 'HIJ'
The regular SQL column sorting order is acceptable for the ranges. There are two main pieces involved here:
- Sending the parameters from .NET to a stored procedure.
- Using the parameters in the SP to do a search.
Some options I have considered, which are not mutually exclusive:
- I could send the string as is. There is no
array[] = SPLIT(',', @query)or similar, so parsing would be low level. I would rather do parsing on the .NET side. - On .NET side, convert to XML, in SP convert to table.
- Use a cursor to go through the parameters that are already in table, do separate queries, and merge the results.
- Create a dynamic where clause (on SQL side with #1, or on .NET side)
Dynamic SQL on the .NET side seems the “easy” way out, but I’m not convinced it’s the best. Any thoughts?
I think creating the SQL statement on the .NET side is more than the easy way out. SQL performs best when doing set-base operations (e.g., running a SELECT statement). SQL is not the best choice when doing procedural coding.
When addressing each of your options other than creating dynamic SQL on .NET side, you are asking SQL to do procedural stuff.
Since you can have .NET do the procedural stuff and SQL Server do the set-based stuff, that’s the way to go.