TOP in mssql

Discussion in 'Databases' started by Jamie18, Sep 11, 2007.

  1. #1
    hey, i can't figure out how to get the top statement to work in mssql.. i'm pretty sure it has some version of it since it turns blue when i type it into the query analyzer..

    when i write something like this

    SELECT TOP 1 Groups.Name
    FROM groups
    WHERE groups.parent_id = someID
    ORDER BY groups.last_update DESC

    it throws an error on the first line. saying "Incorrect syntax near '1'."

    i would just use the set rowcount method, but i want to use this as a subquery
     
    Jamie18, Sep 11, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    anyone have any ideas?

    i'm running ms sql server 2000

    here's another simple example..

    SELECT TOP 1 *
    FROM phone

    gives me the error "Server: Msg 170, Level 15, State 1, Line 1
    Line 1: Incorrect syntax near '1'."

    does anyone know why it's giving me that error? are there any other ways to do this? keeping in mind it will be a subquery.
     
    Jamie18, Sep 11, 2007 IP
  3. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #3
    falcondriver, Sep 11, 2007 IP
  4. jred2002

    jred2002 Well-Known Member

    Messages:
    160
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    If you are in the correct DB it should work with

    SELECT TOP 1 * FROM dbo.phone

    or

    SELECT TOP 1 Name
    FROM dbo.groups
    WHERE parent_id = someID
    ORDER BY last_update DESC
     
    jred2002, Sep 13, 2007 IP