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. Update top row

Update top row

Scheduled Pinned Locked Moved Database
questiondatabasesql-serversysadminannouncement
5 Posts 5 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.
  • D Offline
    D Offline
    dptalt
    wrote on last edited by
    #1

    I want to update the top row in a table.   I'm trying to get this to work: update top (1) mytable set myfield="test" This does not seem to compile.   We have Microsoft SQL Server 2000.   How can I accomplish this?

    L G 2 Replies Last reply
    0
    • D dptalt

      I want to update the top row in a table.   I'm trying to get this to work: update top (1) mytable set myfield="test" This does not seem to compile.   We have Microsoft SQL Server 2000.   How can I accomplish this?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      AFAIK, the update statement does not support the TOP condition. Try something like this;

      UPDATE dbo.myTable
      SET myField = 'test'
      WHERE myPrimaryKeyField = (SELECT TOP 1 myPrimaryKeyField FROM dbo.myTable)

      Hope this helps :)

      I are Troll :)

      I N 2 Replies Last reply
      0
      • D dptalt

        I want to update the top row in a table.   I'm trying to get this to work: update top (1) mytable set myfield="test" This does not seem to compile.   We have Microsoft SQL Server 2000.   How can I accomplish this?

        G Offline
        G Offline
        Geoff Williams
        wrote on last edited by
        #3

        The predicate "TOP 1" applies only to the SELECT statement, not the UPDATE statement. In order to update just the first row in your table, your UPDATE statement will need to be

        UPDATE mytable
        SET myfield = "test"
        WHERE ((SELECT TOP 1 myfield FROM mytable) = myfield);

        (I've split the statement over several lines to improve readability).

        1 Reply Last reply
        0
        • L Lost User

          AFAIK, the update statement does not support the TOP condition. Try something like this;

          UPDATE dbo.myTable
          SET myField = 'test'
          WHERE myPrimaryKeyField = (SELECT TOP 1 myPrimaryKeyField FROM dbo.myTable)

          Hope this helps :)

          I are Troll :)

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #4

          You should have an ORDER BY clause on the subquery to determine the correct TOP 1.

          1 Reply Last reply
          0
          • L Lost User

            AFAIK, the update statement does not support the TOP condition. Try something like this;

            UPDATE dbo.myTable
            SET myField = 'test'
            WHERE myPrimaryKeyField = (SELECT TOP 1 myPrimaryKeyField FROM dbo.myTable)

            Hope this helps :)

            I are Troll :)

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

            A more efficient statement would be this.

            update Table
            set field = new_value
            where PK = (select min(PK) from Table)


            only two letters away from being an asset

            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