What is a good way to handle dynamic sql parameters?
I have a search form that takes in a whole bunch of different search parameters. If the parameters are empty and I have the parameter in the sql string will it screw or slow down the query?
What is a good way to handle dynamic sql parameters?
I have a search form that takes in a whole bunch of different search parameters. If the parameters are empty and I have the parameter in the sql string will it screw or slow down the query?
Depending on the specific implementation, we have two general approaches to this problem:
1) Dynamically build the filter statement for the SQL query in code skipping any parameters that are empty. This is the best approach if you allow the user to select multiple values for a single column (i.e. select 0 or more of the 50 states to filter the data).
For example:
Assuming txtCondition1 and txtCondition2 are textboxes:
2) If the values are more constrained, we usually pass them to a stored procedure, which is responsible for determining whether or not the value is to be evaluated by testing the parameter for "emptinesss", either null, empty string, 0 for numerics, etc.
One of the things that can be done is check whether the parameter was passed to your stored procedure. You can do it like this:
I'd rather do this on sql procedure than in the application tho