Comments on: How to simulate optional parameters in SQL http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Frederick Lin : Optional Parameters in SQL http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/#comment-13562 Frederick Lin : Optional Parameters in SQL Tue, 23 Oct 2007 01:01:47 +0000 http://www.xaprb.com/blog/?p=62#comment-13562 [...] certainly be using this more often.  I found an article the other day explaining how to simulate optional parameters in SQL without duplicating a single SELECT statement several times, each with a different WHERE [...]

]]>
By: Xaprb http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/#comment-3569 Xaprb Mon, 29 Jan 2007 16:57:22 +0000 http://www.xaprb.com/blog/?p=62#comment-3569 There can be. If you enter SMIT% then the database server can do a prefix match, which can use indexes. If you enter %MIT% it becomes a full-length substring search, which is not indexable. So yes, there is always a potential for a bad query to result.

]]>
By: Locusta http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/#comment-3568 Locusta Mon, 29 Jan 2007 16:51:29 +0000 http://www.xaprb.com/blog/?p=62#comment-3568 Hello,

Is there a performance penalty between these 2 statements:

select * from table1
where (@param1 is null or col1 = @param1)
    and (@param2 is null or col2 = @param2)
select * from table1
where (@param1 is null or col1 like @param1)
    and (@param2 is null or col2 like @param2)

This would allow the user to key in e.g. SMIT% to search for a customer name starting with SMIT, but if he keys in SMITH, then only the exact name would appear in the result list.

Is this good practice, or should I avoid?

Thanks for this great article!

]]>
By: Xaprb http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/#comment-1500 Xaprb Tue, 15 Aug 2006 11:13:16 +0000 http://www.xaprb.com/blog/?p=62#comment-1500 DOH! You are right! I’ll amend the article.

]]>
By: core http://www.xaprb.com/blog/2005/12/11/optional-parameters-in-the-where-clause/#comment-1499 core Tue, 15 Aug 2006 10:49:31 +0000 http://www.xaprb.com/blog/?p=62#comment-1499 Heya,

Shouldn’t this:

select * from table1
where (col1 is null or col1 = @param1)
    and (col2 is null or col2 = @param2)

read:

select * from table1
where (@param1 is null or col1 = @param1)
    and (@param2 is null or col2 = @param2)

? Cheers, Will

]]>