Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL query troubles: SELECT [TOP x STARTING AT n]

SQL query troubles: SELECT [TOP x STARTING AT n]

Scheduled Pinned Locked Moved Database
questiondatabasecom
4 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Wulff
    wrote on last edited by
    #1

    I need to be able to do the following using an SQL statement: SELECT TOP 25 * FROM [Table] WHERE [Condition] ORDER BY [Column] Except I want to be able to specify the start position, i.e. start n rows into the table and then extract the 25 rows I need. I don’t know how many rows will be in the table, and there is no field that has any form of incremental value (so I can’t just say [Condition] = [Table].[Column] > n). The table might contain many thousands of rows, so it is unfeasible to retrieve them all then ignore the one’s I don’t want. So, I guess my question is how can I retrieve x rows from the table, starting from row n, based on the results of my ORDER BY clause. This must be possible, surely? David Wulff dwulff@battleaxesoftware.com

    N A 2 Replies Last reply
    0
    • D David Wulff

      I need to be able to do the following using an SQL statement: SELECT TOP 25 * FROM [Table] WHERE [Condition] ORDER BY [Column] Except I want to be able to specify the start position, i.e. start n rows into the table and then extract the 25 rows I need. I don’t know how many rows will be in the table, and there is no field that has any form of incremental value (so I can’t just say [Condition] = [Table].[Column] > n). The table might contain many thousands of rows, so it is unfeasible to retrieve them all then ignore the one’s I don’t want. So, I guess my question is how can I retrieve x rows from the table, starting from row n, based on the results of my ORDER BY clause. This must be possible, surely? David Wulff dwulff@battleaxesoftware.com

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      I know you can use SET ROWCOUNT to limit the number of effected rows. I'm not sure if there is anything to set the start position. Interesting question though.

      1 Reply Last reply
      0
      • D David Wulff

        I need to be able to do the following using an SQL statement: SELECT TOP 25 * FROM [Table] WHERE [Condition] ORDER BY [Column] Except I want to be able to specify the start position, i.e. start n rows into the table and then extract the 25 rows I need. I don’t know how many rows will be in the table, and there is no field that has any form of incremental value (so I can’t just say [Condition] = [Table].[Column] > n). The table might contain many thousands of rows, so it is unfeasible to retrieve them all then ignore the one’s I don’t want. So, I guess my question is how can I retrieve x rows from the table, starting from row n, based on the results of my ORDER BY clause. This must be possible, surely? David Wulff dwulff@battleaxesoftware.com

        A Offline
        A Offline
        Anders Molin
        wrote on last edited by
        #3

        You can not do that, sorry. (I have also needed it) - Anders Money talks, but all mine ever says is "Goodbye!"

        D 1 Reply Last reply
        0
        • A Anders Molin

          You can not do that, sorry. (I have also needed it) - Anders Money talks, but all mine ever says is "Goodbye!"

          D Offline
          D Offline
          David Wulff
          wrote on last edited by
          #4

          I had guessed as much :(. Why oh why did the original designers of SQL not think people would need that functionality? It's common sense if you just want to retrieve, say, 20 rows from a fifty million row table, that you would rather not return the first 49,999,980 only to ignore them. David Wulff dwulff@battleaxesoftware.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups