locked
How this pagination code would be working RRS feed

  • Question

  • i just read a article on pagination of records in sql server from this link http://www.codeproject.com/Articles/590341/Stored-Procedure-with-Sorting-Paging-and-Filtering

    http://sqlwithmanoj.com/2011/12/30/creating-stored-procedures-with-dynamic-search-paging-pagination/

    please see their pagination script and specially look at the where area

     WHERE
        (@lContactID IS NULL OR ContactID = @lContactID)
        AND(@lFirstName IS NULL OR FirstName LIKE ‘%’ + @lFirstName + ‘%’)
        AND(@lLastName IS NULL OR LastName LIKE ‘%’ + @lLastName + ‘%’)
        AND (@lEmailAddress IS NULL OR EmailAddress LIKE ‘%’ + @lEmailAddress + ‘%’)
        AND (@lEmailPromotion IS NULL OR EmailPromotion = @lEmailPromotion)
        AND (@lPhone IS NULL OR Phone  LIKE ‘%’ +@lPhone+ ‘%’)

    how the above where clause will work ?

    they use AND clause instead of OR.

    they wrote like if contactid is null or ContactID = @lContactID and again they use LastName IS NULL OR LastName LIKE ‘%’ + @lLastName + ‘%’

    anyone can help me to understand how their where clause will work when user send no value for those fields in where clause or may be user will send multiple value for different fields in where clause.

    please help. thanks


    • Edited by Mou_kolkata Tuesday, January 5, 2016 12:01 PM
    Wednesday, December 16, 2015 3:17 PM

Answers

  • First of all: You should choose a better subject, cause pagination is not the real topic.

    Then 'no value' is not a term we should use. Say NULL or empty string or value.

    And for your question: It's a simple catch-all (the inner OR) condition. Where the different search criteria are additive. So when you search for first and last name, that only rows are returned where both single conditions are true.

    • Proposed as answer by Naomi N Wednesday, December 16, 2015 5:19 PM
    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 3:41 PM
  • Hi

    If you take a closer look at store proc it uses commontableExpress and also rownumber function of sql.

    Rownumber function is used in table expression which gives a result set with record no and based on this record set and based on parameters like pagesize n page no table get filtered futher

    Ref

    Common table expression from sql authority

    Row_number

    If you find this answer helpful, Kindly marked as answer

    Thanking you,

    Shridhar J Joshi


    Shridhar J Joshi Thanks a lot

    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 4:01 PM
  • Stefan explained correctly how the logic works. You may also want to take a look at the following blog post

    Do you use Column=@Param OR @Param IS NULL in your WHERE clause? Don’t, it doesn’t perform

    and also Gail Shaw's blog post

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

    The solution for SQL Server 2008 R2 and up will be to add OPTION (RECOMPILE) at the end of the query. 


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 5:24 PM

All replies

  • First of all: You should choose a better subject, cause pagination is not the real topic.

    Then 'no value' is not a term we should use. Say NULL or empty string or value.

    And for your question: It's a simple catch-all (the inner OR) condition. Where the different search criteria are additive. So when you search for first and last name, that only rows are returned where both single conditions are true.

    • Proposed as answer by Naomi N Wednesday, December 16, 2015 5:19 PM
    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 3:41 PM
  • Hi

    If you take a closer look at store proc it uses commontableExpress and also rownumber function of sql.

    Rownumber function is used in table expression which gives a result set with record no and based on this record set and based on parameters like pagesize n page no table get filtered futher

    Ref

    Common table expression from sql authority

    Row_number

    If you find this answer helpful, Kindly marked as answer

    Thanking you,

    Shridhar J Joshi


    Shridhar J Joshi Thanks a lot

    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 4:01 PM
  • Stefan explained correctly how the logic works. You may also want to take a look at the following blog post

    Do you use Column=@Param OR @Param IS NULL in your WHERE clause? Don’t, it doesn’t perform

    and also Gail Shaw's blog post

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

    The solution for SQL Server 2008 R2 and up will be to add OPTION (RECOMPILE) at the end of the query. 


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Mou_kolkata Saturday, December 19, 2015 7:02 AM
    Wednesday, December 16, 2015 5:24 PM