Search Query

  • Hi,

    I have to write a query for searching records.

    I have four input fields.

    If USER is entering value in one

    input field data to be serached on that specific single field.

    If more than one entry then all parameters should be ANDed.

    I don't want to use if A='' Then Do this

    Please suggest me any good idea.

    Shatrughna

  • 'Catch all' ,follow the link

    http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

    ---------------------------------------------------------------------------------

  • You could try this:

    SELECT *

    FROM dbo.SomeTable

    WHERE ( @Field1 IS NULL OR Field1 = @Field1 )

    AND ( @Field2 IS NULL OR Field2 = @Field2 )

    AND ( @Field3 IS NULL OR Field3 = @Field3 )

    AND ( @Field4 IS NULL OR Field4 = @Field4 )

    Provided that the search fields are NULL when unused and non-NULL when used.

    CEWII

  • Thanks.

    Appreciated.

    Shatrughna

  • Elliott W (11/17/2009)


    You could try this:

    SELECT *

    FROM dbo.SomeTable

    WHERE ( @Field1 IS NULL OR Field1 = @Field1 )

    AND ( @Field2 IS NULL OR Field2 = @Field2 )

    AND ( @Field3 IS NULL OR Field3 = @Field3 )

    AND ( @Field4 IS NULL OR Field4 = @Field4 )

    Provided that the search fields are NULL when unused and non-NULL when used.

    You could, providing that performance is not a major concern. Take a look at the link that Nabha posted.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • G,

    I got the parameter sniffing vibe off that article.. I have always had really good performance off these kinds of queries because I have avoided the causes of that. As far as performance the where clause only gets evaluated once. The optimizer knows that if the variable is NULL that is already has a positive result so it doesn't even have to use that in the plan..

    I tend to think this is better than dynamic SQL.. But I may do some additional testing..

    CEWII

  • Elliott W (11/17/2009)


    I got the parameter sniffing vibe off that article..

    It's not so much parameter sniffing. It's that there's no single optimal execution plan for that kind of query and, because the optimiser has to pick a safe plan, it generally picks a sub-optimal one.

    The optimizer knows that if the variable is NULL that is already has a positive result so it doesn't even have to use that in the plan..

    Not really. Optimiser doesn't deal well with multiple '@variable is null OR' constructs. Plus it can't (except in 2008 when OPTION RECOMPILE is specified) not use a particular branch in the plan when a parameter is null, because that plan will be cached for reuse and next time the query runs the parameter may not be null. If it made that assumption, the plan would result in incorrect results sometimes. That's not allowed, the plan must always be safe for reuse.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply