Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Posts
    69

    Unanswered: LIMIT rows with an offset

    I have a query in MySQL which needs to be translated to an equivalent in MSSQL Server. The query uses a LIMIT clause with an offset.

    select * from test LIMIT 10,5

    (meaning from the 10th row containing 5 rows in the result set)

    How can we port the same logic to SQL Server ? I know TOP can retrieve top n rows but how can we specify the offset ?

    Any help is appreciated !

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Any such offsets or paging is meaningless without an ORDER BY clause.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Blindman is quite correct, without an ORDER BY clause this is dangerous in any SQL dialect.

    SQL-2000 doesn't have a good way to implement the functionality of MySQL's LIMIT clause. Until a new version of SQL provides a better solution, the best answer I can give you is that your code needs to provide the paging functionality.

    The best way that I know to provide paging functionality is to use ORDER BY with a "rowset key" within your data and the TOP N syntax to limit the number of rows returned by a SELECT statement. Keep track of the rowset key of the last row returned, and use that as the seed for the next iteration (page) of your SELECT.

    -PatP

  4. #4
    Join Date
    Apr 2004
    Posts
    69
    Appreciate your help ! Thanks !

  5. #5
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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