+ Reply to Thread
Results 1 to 7 of 7

Thread: Equivalent of Limit in MySql

  1. #1
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Equivalent of Limit in MySql

    Hello
    I need to do paging, but cant seem to able to get query right in Sql Server 2000/2005

    In MySql, it was simple, just used Limit

    Anything like that in Sql Server or is it complicated here??

    Thanx

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by micky View Post
    Hello
    I need to do paging, but cant seem to able to get query right in Sql Server 2000/2005

    In MySql, it was simple, just used Limit

    Anything like that in Sql Server or is it complicated here??

    Thanx
    M, if you have SQL Server 2005 or higher you can do something like this:
    Code:
    SELECT * FROM
    (
        SELECT
             [column1]
            ,[column2]
            ,[column3]
            ,ROW_NUMBER() OVER (ORDER BY [column2] DESC) AS RowNum
        FROM table_name
    ) TableName
    WHERE TableName.RowNum BETWEEN (@PageIndex*@PageSize) AND ((@PageIndex+1) *@PageSize)
    ORDER BY [column2] DESC
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Also, if your table happens to have a Primary Key which is an auto increment value:
    Code:
    Select Top (@pagesize) *
       From Table
       Where keyColumn > (@pageindex * @pageSize)
    
    might work. Assuming no gaps in the sequence and you don't have other selection criteria. (i.e. this would work on a Report Cache table for example)
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  4. #4
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Hmm, i saw that in some links......... kind of complicated with my query

    Ok, i'll try it tomorrow and get back

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    M, just be sure that you do all your logic in the inner query:
    Code:
        SELECT
             [column1]
            ,[column2]
            ,[column3]
            ,ROW_NUMBER() OVER (ORDER BY [column2] DESC) AS RowNum
        FROM table_name
    
    Leave the outer query alone and it shouldn't be a problem.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  6. #6
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    J, i have a query like this
    Code:
    strSQL = "SELECT T.Team_Id, T.Team_Name, SP.Sport_Name, S.School_Name, C.Class_Name, D.District_Name, ST.State_Name, CT.City_Name " & _
    "FROM tblTeam as T, tblSport as SP, tblSchool as S, tblClass as C, tblDistrictMaster as D, tblStateMaster as ST, tblCityMaster as CT " & _
    "WHERE T.Sport_Id=SP.Sport_Id and T.School_Id=S.School_Id and S.State_Id=ST.State_Id And S.City_Id=CT.City_Id " & _
    "and S.Class_Id=C.Class_Id and S.District_Id=D.District_Id  ORDER BY T.Team_Name"
    
    I need to show paging on this query
    Last edited by micky; July 24th, 2009 at 04:27 AM.

  7. #7
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    ok, i got it working
    Code:
    Select * From
    (SELECT T.Team_Id, T.Team_Name, SP.Sport_Name, S.School_Name, C.Class_Name, D.District_Name, ST.State_Name, CT.City_Name, 
    ROW_NUMBER() OVER (ORDER BY Team_Name, School_Name) AS RowNum
    FROM tblTeam as T, tblSport as SP, tblSchool as S,
    tblClass as C, tblDistrictMaster as D, tblStateMaster as ST, tblCityMaster as CT
    WHERE T.Sport_Id=SP.Sport_Id and T.School_Id=S.School_Id and S.State_Id=ST.State_Id And S.City_Id=CT.City_Id
    and S.Class_Id=C.Class_Id and S.District_Id=D.District_Id) 
    TableName WHERE TableName.RowNum > 0 and TableName.RowNum <= 50 
    Thanx J

+ Reply to Thread

Similar Threads

  1. PHP equivalent to MySQL IN statement?
    By bryceowen in forum PHP Development
    Replies: 2
    Last Post: July 8th, 2009, 04:01 PM
  2. Limit Checkbox Items Checked
    By todd2006 in forum JavaScript Programming
    Replies: 16
    Last Post: April 8th, 2009, 05:44 AM
  3. How to limit the amount of pictures on one page??
    By dtz in forum ASP Development
    Replies: 34
    Last Post: February 25th, 2009, 04:24 PM
  4. WebService return limit
    By richyrich in forum .NET Development
    Replies: 1
    Last Post: December 12th, 2008, 02:20 PM
  5. UBound and LBound equivalent for PHP?
    By bryceowen in forum PHP Development
    Replies: 1
    Last Post: September 12th, 2008, 10:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO