Greetings all!
We are porting some search functionality from an existing VB6 app to a web-based app. There is a form to allow the user to perform an advanced search on the data. In this case, there are 20+ different criteria elements that can be part of the query.
In the current application (VB6), this query is built dynamically in VB.
In the new application, the first pass was to create a stored procedure that accepted all of the parameters and then use the following shortcut for determining which criteria were used:
...
( @p_LastName IS NULL or v.Last_Name like @p_LastName + '%') AND
...
We also experimented with the COALESCE() method but were coming up with different results due to the way nulls were implemented in the table. Both of these methods are fairly common shortcuts for handling a bunch of parameters.
However, another project ran into performance issues with the first approach due to the fact that the execution plan is cached based on the first set of parameters that come in. This is also mentioned in the comments in the great post "Stored procedures are bad, m'kay?" - http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx post by Frans way back. Search for "with recompile" on the article.
To avoid those issues, as well as some slight result differences that we were experiencing, the direction given was to use the same logic from the VB6 app to dynamically build the SQL in the new app.
What was actually implemented was to dynamically build the SQL statement inside the stored procedure. Now, I've done dynamic SQL in my apps, used plenty of stored procedures, and also implemented solutions with LLBL. I have never built SQL inside a stored procedure. Asking around a little, there are a number of people on the team who have done it this way in the past.
My question(s): Is this a valid way of doing things? What are the ramifications? Pros? Cons? It seems to only allow you to explicitly know how the query will work (vs. coalesce and null compares) but you would still have potential performance issues because you still have a variable number of parameters going into the stored procedure. Plus, it seems like such a pain to code that kind of logic in SQL versus C#.
I'm hoping that the reason I haven't encountered it before is because it is a not so good practice that should be avoided.
Thoughts?