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. Select number of rows [modified]

Select number of rows [modified]

Scheduled Pinned Locked Moved Database
databasehelp
8 Posts 3 Posters 0 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.
  • M Offline
    M Offline
    MathewPV
    wrote on last edited by
    #1

    I have a table with 5000 rows I want to get the first 1000 rows in a single select query and the next 1001 to 5000 rows in another select query. A help from somebody will be deeply appreciated.

    modified on Tuesday, August 3, 2010 11:34 PM

    J J 2 Replies Last reply
    0
    • M MathewPV

      I have a table with 5000 rows I want to get the first 1000 rows in a single select query and the next 1001 to 5000 rows in another select query. A help from somebody will be deeply appreciated.

      modified on Tuesday, August 3, 2010 11:34 PM

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Which database server and version?

      M 1 Reply Last reply
      0
      • J J4amieC

        Which database server and version?

        M Offline
        M Offline
        MathewPV
        wrote on last edited by
        #3

        Sql server 2005

        1 Reply Last reply
        0
        • M MathewPV

          I have a table with 5000 rows I want to get the first 1000 rows in a single select query and the next 1001 to 5000 rows in another select query. A help from somebody will be deeply appreciated.

          modified on Tuesday, August 3, 2010 11:34 PM

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          The first one is easy enough.

          SELECT TOP 1000 * FROM MYTABLE

          The other one could probably be done like this assuming you have a Primary Key MYTABLEID (or some other unique key):

          SELECT *
          FROM MYTABLE
          WHERE MYTABLEID IN (SELECT TOP 5000 MYTABLEID FROM MYTABLE)
          AND MYTABLEID NOT IN (SELECT TOP 1000 MYTABLEID FROM MYTABLE)

          "When did ignorance become a point of view" - Dilbert

          M J 2 Replies Last reply
          0
          • J Jorgen Andersson

            The first one is easy enough.

            SELECT TOP 1000 * FROM MYTABLE

            The other one could probably be done like this assuming you have a Primary Key MYTABLEID (or some other unique key):

            SELECT *
            FROM MYTABLE
            WHERE MYTABLEID IN (SELECT TOP 5000 MYTABLEID FROM MYTABLE)
            AND MYTABLEID NOT IN (SELECT TOP 1000 MYTABLEID FROM MYTABLE)

            "When did ignorance become a point of view" - Dilbert

            M Offline
            M Offline
            MathewPV
            wrote on last edited by
            #5

            Thank you for the valuable information. Yes it worked

            J 1 Reply Last reply
            0
            • M MathewPV

              Thank you for the valuable information. Yes it worked

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Please stop bolding every post. This is the right way to do paging with SQL 2005: http://www.davidhayden.com/blog/dave/archive/2005/12/30/2652.aspx[^]

              1 Reply Last reply
              0
              • J Jorgen Andersson

                The first one is easy enough.

                SELECT TOP 1000 * FROM MYTABLE

                The other one could probably be done like this assuming you have a Primary Key MYTABLEID (or some other unique key):

                SELECT *
                FROM MYTABLE
                WHERE MYTABLEID IN (SELECT TOP 5000 MYTABLEID FROM MYTABLE)
                AND MYTABLEID NOT IN (SELECT TOP 1000 MYTABLEID FROM MYTABLE)

                "When did ignorance become a point of view" - Dilbert

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                Although this is how it used to be done pre 2005, in SQL Server 2005 there was the ROW_NUMBER function which enabled much more efficient paging queries to be written.

                J 1 Reply Last reply
                0
                • J J4amieC

                  Although this is how it used to be done pre 2005, in SQL Server 2005 there was the ROW_NUMBER function which enabled much more efficient paging queries to be written.

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  Quite similar to Oracle then, which still lacks the TOP function.

                  "When did ignorance become a point of view" - Dilbert

                  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