tags:

views:

185

answers:

2

I would like to use following sql to avoid constructing sql dynamically:

SELECT CommentID, Comment, 
FROM Comments
--if Author id is not null then filter upon author id otherwise get all comments (ignore author id)
WHERE AuthorID LIKE COALESCE(@AuthorId, '%') 
   --if comment type is present filter upon it, otherwise get all comments (ignore comment type condition)
   AND CommentType LIKE COALESCE(@CommentType, '%') 

I want to know is that safe way to approach this problem?

EDIT: Here is final code that satisfy my need to ignore search parameter if is null and applied it if is present:

SELECT CommentID, Comment, 
FROM Comments
--if @AuthorId is not null then filter upon @AuthorId otherwise get all comments (ignore author id)
WHERE AuthorID = COALESCE(@AuthorId, AuthorID) 
    --if @CommentType is present filter upon it, otherwise get all comments (ignore comment type condition)
    AND CommentType = COALESCE(@CommentType, CommentType)  
A: 

Looks good to me but in this particular case you can ISNULL instead of COALESCE.

Just thought that I would point out that I don't think you will return any values if the AuthorID or CommentType is null.

wcm
thanks for fast response, I'll wait to see if there are some more opinions and approve your answer ...
krul
ISNULL and COALESCE effectively do the same thing except that COALESCE can handle more than two parameters. In addition, I would recommend against ISNULL since COALESCE is the ANSI standard.
Thomas
I didn't see Thomas' answer before posting my edit. Honest!
wcm
OK, to conclude, since no field is nullable in my comments table I can assume that code will safely include condition if condition is not null or ignore condition if condition is null?
krul
Did not know that about COALESCE :) I'll start using that more myself. I have recently started doing a lot more Oracle stuff.
wcm
+3  A: 

If AuthorId or CommentType are nullable, this will not work. Instead you should use an OR:

SELECT CommentID, Comment, 
FROM Comments
WHERE ( @AuthorId Is Null Or AuthorId = @AuthorId )
    And ( @CommentType Is Null Or CommentType Like @CommentType )

Btw, if AuthorId and CommentType are not nullable, then you can use Coalesce like so:

SELECT CommentID, Comment, 
FROM Comments
WHERE AuthorId = Coalesce(@AuthorId, AuthorId)
    And CommentType = Coalesce(@CommentType, CommentType )

The catch is that this is an exact match as opposed to a wildcard match like you had with LIKE.

Thomas
No field in Comments table is nullable, I beleive that I'm safe with proposed code?
krul
@krul - If no field on which you are filtering is nullable, then you can use Coalesce but it will result in an exact match. If you need a wildcard match, then you need to use the first approach I provided.
Thomas
@krul - You can of course mix and match. Use Coalesce for exact match columns and use the `@Foo Is Null Or Foo Like @Foo` construct for those where you want a wildcard match.
Thomas
Thanks, that clarifies issue, due to explaining in-proper use of wild card in my code I will grant you answered check.
krul
Would you say that your code is more performant than the COALESCE option that Krul posed in his question?
wcm
@wcm - I would guess that my solution would be faster in that it does not actually have to perform the LIKE comparison when the parameter is null. However, if there are lots of AND statements, it can start to be a problem (in either solution). If that happens, then an alternate solution such as free-text should be sought.
Thomas