all groups > sql server programming > november 2004 >
You're in the

sql server programming

group:

Stored Proc Query with Conditional filters


Stored Proc Query with Conditional filters WJ
11/18/2004 10:03:40 PM
sql server programming:
I need to create a proc that searches a table. However, I want to have
conditional filtering on it.

For example, if I have a person table, and the user types in a filter for
the LastName field like 'S%'
it will return all records where the LastName begins with S.
However, if the user also enters Height between 5 & 6 then that will be
added as well.

So the first query will look like

Select * from tblPerson where LastName like 'S%'

the next query will be

Select * from tblPerson where LastName like 'S%' and Height Between
@MinHeight and @MaxHeight

Here's my ordeal. If the filter is not passed, I don't want it in the where
clause (e.g. the height).
My table has over 30 fields that can be filtered on.

One way to do this is to generate the SQL dynamically and then exec it.
That works. But my concern is
this query will be used heavily. The Dynamic SQL will not be as efficient
as a compiled query, since
SQL will have to generate the execution plan each time.

Is there another way I can test the params passed in, and if both @MinHeight
and @MaxHeight are both
null, then omit them from the where clause?

Thanks!

Re: Stored Proc Query with Conditional filters oj
11/18/2004 10:09:03 PM
You very much answer yourself. Though, I suggest taking a look at Erland's
article...

http://www.sommarskog.se/dyn-search.html


[quoted text, click to view]

Re: Stored Proc Query with Conditional filters WJ
11/18/2004 10:53:05 PM

[quoted text, click to view]

Thanks David. Actually, I was going to exec the Dynamic SQL from a stored
procedure, so I'd still
have to only grant exec to the proc to users.

Many of the fields being queries are ints and the most common have indexes.
The ones that are non-numeric
are varchar(255) and the user can only search with a BEGINS with filter
(i.e. @Filter+'%'). No
contains with a wildcard before and after the string being searched for. I
think that will help offset some
performance concerns.

Thanks for the NOLOCK hint. That's a good idea. I'll do that.


Re: Stored Proc Query with Conditional filters David Gugick
11/19/2004 1:23:00 AM
[quoted text, click to view]

Dynamic SQL can reuse execution plans. You need to use sp_executesql and
bind the parameters into the query. Assuming you get similar executions,
the exeuction plans should be reused. The caveat being that select
access to the underlying tables needs to be granted to the users.

Given that you have 30 columns and a large number of possible executions
(it appears you are going to allow any combination) , I would use
dynamic SQL.

It's not like queries that say "Where (col = @col_val or @col_val is
null)" for 30 columns will be very efficient. I would be more concerned
about users entering criteria that won't be helped by an index or that
return too many rows. Consider using WITH (NOLOCK) on your queries to
prevent unnecessary locking. At least, that way, a big select statement
won't acquire shared locks on the tables.


--
David Gugick
Imceda Software
www.imceda.com
Re: Stored Proc Query with Conditional filters Tibor Karaszi
11/19/2004 8:49:13 AM
[quoted text, click to view]

No, when you are using dynamic SQL, the user need permission to do whatever the string you execute
does.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

Re: Stored Proc Query with Conditional filters David Gugick
11/19/2004 11:41:38 AM
[quoted text, click to view]

As Tibor mentioned, user require security access to the underlying
tables in order to use dynamic SQL, even from stored procedures. You
think if you were limited to selecting data, you could grant them select
access to a view on the table.

You are going to need to do some really good testing to test for
performance in all possible conditions. Remember, if SQL Server
determines that a high percentage of rows are to be returned (and that
value could be as low as 5%), it may opt to use a table scan / clustered
index scan.

But you do have the opportunity to do some real tweaking if you are
ambitious. If you find SQL Server does not always use the proper index
strategy, and it might not given the breadth of variables, you can add
index hints to the SQL statement. For example, if you determine that a
column is being used that you know is highly selective, you could add an
index hint when that column is used in the dynamic SQL query. I'm
generally not big on index hints, but if you see performance that falls
short of acceptible, you have options.

You also can employ things like temp tables to pull out keys when highly
selectable columns are used and join the temp table with the main query.
The point is, with dynamic SQL, you have a lot of options.

You could also turn this into a client component if you find the coding
is lenghy. While it will make updates for involved, it will give you a
real language which which to program and you can still use
sp_executesql.

Lot of options.


--
David Gugick
Imceda Software
www.imceda.com
Re: Stored Proc Query with Conditional filters WJ
11/20/2004 10:57:46 PM
Excellent. Thanks for all the feedback. Points well taken.

I forgot about granting select for the dynamic SQL.

Thanks again!


AddThis Social Bookmark Button