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. using LIMIT within a SELECT statment

using LIMIT within a SELECT statment

Scheduled Pinned Locked Moved Database
csharpasp-netdatabasemysqlsql-server
4 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
    Matt Newman
    wrote on last edited by
    #1

    I am trying to limit the number of items returned from a SQL Server (SQL Server 2000). However when I do something like this ASP.NET gets mad at me:

    SELECT * FROM SomeWhere WHERE Something = 'SomeCondition' LIMIT 5

    Basically I only want the first 5 results returned. This worked in my experience in MySQL. Could anyone show me how to do it in SQL Server 2000? Matt Newman

    H 1 Reply Last reply
    0
    • M Matt Newman

      I am trying to limit the number of items returned from a SQL Server (SQL Server 2000). However when I do something like this ASP.NET gets mad at me:

      SELECT * FROM SomeWhere WHERE Something = 'SomeCondition' LIMIT 5

      Basically I only want the first 5 results returned. This worked in my experience in MySQL. Could anyone show me how to do it in SQL Server 2000? Matt Newman

      H Offline
      H Offline
      Henrik Stuart
      wrote on last edited by
      #2

      SELECT TOP 5 * FROM SomeWhere WHERE Something = 'SomeCondition'

      MSSQL doesn't support "LIMIT x, y" but it can be emulated through a somewhat... icky... nested subselection. -- Henrik Stuart (http://www.unprompted.com/hstuart/)

      M 1 Reply Last reply
      0
      • H Henrik Stuart

        SELECT TOP 5 * FROM SomeWhere WHERE Something = 'SomeCondition'

        MSSQL doesn't support "LIMIT x, y" but it can be emulated through a somewhat... icky... nested subselection. -- Henrik Stuart (http://www.unprompted.com/hstuart/)

        M Offline
        M Offline
        Matt Newman
        wrote on last edited by
        #3

        Thanks, thats exactly what I needed. Matt Newman

        L 1 Reply Last reply
        0
        • M Matt Newman

          Thanks, thats exactly what I needed. Matt Newman

          L Offline
          L Offline
          Lee Fuller
          wrote on last edited by
          #4

          Also consider set rowcount n to stop processing of the result set after n rows (and set rowcount 0 to reset). The set rowcount is especially useful in a stored proc where you want to pass n (the # of rows) as a variable from your components/application. Cheers, Lee. Lee H Fuller lee@fullerdata.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